C# “A(IListx)其中T:I”和“A(IListx)”之间的区别?
两者的区别是什么C# “A(IListx)其中T:I”和“A(IListx)”之间的区别?,c#,generics,methods,types,interface,C#,Generics,Methods,Types,Interface,两者的区别是什么 public void MyMethod<T>(IList<T> myParameter) where T : IMyInterface 及 ?IList不是,因此无法将IList传递给第二个方法 假设你可以,你有: class MyClass1 : IMyInterface {} class MyClass2 : IMyInterface {} MyMethod的实施是: 如果你想打电话 MyMethod(new List<MyClass1&
public void MyMethod<T>(IList<T> myParameter) where T : IMyInterface
及
?IList不是,因此无法将IList传递给第二个方法
假设你可以,你有:
class MyClass1 : IMyInterface {}
class MyClass2 : IMyInterface {}
MyMethod的实施是:
如果你想打电话
MyMethod(new List<MyClass1>()) ;
它将在运行时失败,因为列表被定义为包含MyClass1对象,并且无法保存MyClass2对象。IList不是,因此您无法将IList传递给第二个方法
假设你可以,你有:
class MyClass1 : IMyInterface {}
class MyClass2 : IMyInterface {}
MyMethod的实施是:
如果你想打电话
MyMethod(new List<MyClass1>()) ;
它将在运行时失败,因为列表被定义为包含MyClass1对象,并且不能保存MyClass2对象。回答得很好。措辞和布局都很好。回答得很好。措辞和布局都很好。一个是一般的,另一个不是:但这显然不是你要问的-你能评论/接受D Stanley的答案,这样你就清楚你在寻找什么。一个是一般的,另一个不是:但这显然不是你要问的-你能评论/接受D Stanley的答案,这样你就清楚你是什么吗寻找。