public interface IMyInterface
{
List<string> MyList(string s)
}
public class MyClass : IMyInterface
{
public List<string> MyList(string s)
}
的實現之間有什麼區別:C# - 如何正確使用接口
[Method]
MyClass inst = new MyClass();
...
或者:
[Method]
var inst = new MyClass() as IMyInterface;
...
或者:
[Method]
IMyInterface inst = new MyClass();
...
是什麼使用IMyInterface的實現的正確方法?
所以我應該使用:MyClass inst = new MyClass(); ? – user1481183
@ user1481183:這取決於您的代碼*是否需要*使用MyClass特定的成員,或者是否可以使用該接口。 –
因此,如果我的代碼需要使用MyClass特定的成員,請使用第一個選項,否則使用第三個選項? – user1481183