Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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# 此列表中有哪些错误<;列表>;?_C#_.net_List_Declaration - Fatal编程技术网

C# 此列表中有哪些错误<;列表>;?

C# 此列表中有哪些错误<;列表>;?,c#,.net,list,declaration,C#,.net,List,Declaration,这似乎是正确的: IList<IList<string>> MyList = new List<IList<string>>(); IList<string> List_Temp = new List<string>(); MyList .Add(List_Temp ); IList MyList=new List(); IList List_Temp=新列表(); 我的列表添加(列表温度); 这似乎不正确: IList

这似乎是正确的:

IList<IList<string>> MyList = new List<IList<string>>();
IList<string> List_Temp = new List<string>();
MyList .Add(List_Temp );
IList MyList=new List();
IList List_Temp=新列表();
我的列表添加(列表温度);
这似乎不正确:

IList<IList<string>> MyList = new List<IList<string>>();
IList<string> List_Temp = new List<string>();
MyList .Add(List_Temp );
IList<List<string>> MyList = new List<List<string>>();
IList<string> List_Temp = new List<string>();
MyList .Add(List_Temp );
IList MyList=new List();
IList List_Temp=新列表();
我的列表添加(列表温度);

为什么第二个不正确?

因为您正在尝试添加一些
IList
实现istead的
List
类,这是定义所要求的-IList。看看这个:

IList<List<string>> MyList = new List<List<string>>();
IList<string> List_Temp = new Collection<string>(); // ooops!
MyList .Add(List_Temp );
IList MyList=new List();
IList List_Temp=new Collection();//哎呀!
我的列表添加(列表温度);

示例的第二行是正确的,因为
Collection
实现了
IList
,但第三行是错误的,因为
Collection
没有继承
List

,因为您试图添加
List
类的一些
IList
实现istead,这是定义上的需求-IList。看看这个:

IList<List<string>> MyList = new List<List<string>>();
IList<string> List_Temp = new Collection<string>(); // ooops!
MyList .Add(List_Temp );
IList MyList=new List();
IList List_Temp=new Collection();//哎呀!
我的列表添加(列表温度);

示例的第二行是正确的,因为
Collection
实现了
IList
,但第三行是错误的,因为
集合
不继承
列表

MyList
包含类型为
列表
的元素,但是您尝试添加类型为
IList的元素

MyList
包含类型为
列表
的元素,但您正在尝试添加类型为的元素
IList

编译器说它不能将
“System.Collections.Generic.IList”
强制转换为
“System.Collections.Generic.List”

列表
定义如下:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
公共类列表:IList、ICollection、IEnumerable、IList、ICollection、IEnumerable
IList

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
公共接口IList:ICollection、IEnumerable、IEnumerable

因此,
List
可以转换为
IList
。反之亦然。

编译器说它不能将
“System.Collections.Generic.IList”
强制转换为
“System.Collections.Generic.List”

列表
定义如下:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
公共类列表:IList、ICollection、IEnumerable、IList、ICollection、IEnumerable
IList

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
公共接口IList:ICollection、IEnumerable、IEnumerable

因此,
List
可以转换为
IList
。反之亦然。

每次将mylist对象类型添加到列表中时,对象类型应相同mylist对象类型为list,并且您用于在其中添加iList

每次将mylist对象类型添加到列表中时,对象类型应相同mylist对象类型为list,并且您用于在其中添加iList对象类型时,对象类型应相同您正在将mylist对象类型添加到列表中,并且每次将mylist对象类型添加到列表中时,您用于将iList添加到itobject类型中的对象类型都应相同,并且您用于将iList添加到列表中,以便列表变为集合或列表?@markzzz:a
Collection
。变量
List_Temp
引用的对象的实际类型在赋值运算符的右侧定义。变量
List_Temp
的类型定义为
IList
。请注意,这些类型通常不等效。但是,当然,变量的类型必须是可从对象类型赋值的?优点是什么?你能给我举个例子吗?我从来都不明白。。。(我曾经是一名Java程序员)@markzzz:例如,
List\u Temp
是类成员。我想用构造函数中的
Collection
来初始化它,我想用
IList
来限制代码的其余部分,我想用IList来限制代码的其余部分“为什么?那是我不明白的。如果我知道它将成为一个集合,我只是将它声明为集合:)而不是通过接口:OSo该列表将成为一个集合还是一个列表?@markzzz:a
Collection
,当然。变量
List_Temp
引用的对象的实际类型在赋值运算符的右侧定义。变量
List_Temp
的类型定义为
IList
。请注意,这些类型通常不等效。但是,当然,变量的类型必须是可从对象类型赋值的?优点是什么?你能给我举个例子吗?我从来都不明白。。。(我曾经是一名Java程序员)@markzzz:例如,
List\u Temp
是类成员。我想用构造函数中的
Collection
来初始化它,我想用
IList
来限制代码的其余部分,我想用IList来限制代码的其余部分“为什么?那是我不明白的。如果我知道它将成为一个集合,我只是将它声明为集合:)而不是通过接口:O