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:aCollection
。变量List_Temp
引用的对象的实际类型在赋值运算符的右侧定义。变量List_Temp
的类型定义为IList
。请注意,这些类型通常不等效。但是,当然,变量的类型必须是可从对象类型赋值的代码>?优点是什么?你能给我举个例子吗?我从来都不明白。。。(我曾经是一名Java程序员)@markzzz:例如,List\u Temp
是类成员。我想用构造函数中的Collection
来初始化它,我想用IList
来限制代码的其余部分,我想用IList来限制代码的其余部分“为什么?那是我不明白的。如果我知道它将成为一个集合,我只是将它声明为集合:)而不是通过接口:OSo该列表将成为一个集合还是一个列表?@markzzz:aCollection
,当然。变量List_Temp
引用的对象的实际类型在赋值运算符的右侧定义。变量List_Temp
的类型定义为IList
。请注意,这些类型通常不等效。但是,当然,变量的类型必须是可从对象类型赋值的代码>?优点是什么?你能给我举个例子吗?我从来都不明白。。。(我曾经是一名Java程序员)@markzzz:例如,List\u Temp
是类成员。我想用构造函数中的Collection
来初始化它,我想用IList
来限制代码的其余部分,我想用IList来限制代码的其余部分“为什么?那是我不明白的。如果我知道它将成为一个集合,我只是将它声明为集合:)而不是通过接口:O