Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “A(IListx)其中T:I”和“A(IListx)”之间的区别?_C#_Generics_Methods_Types_Interface - Fatal编程技术网

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的答案,这样你就清楚你是什么吗寻找。