初始化 IList<T> C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1349012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:29:58  来源:igfitidea点击:

Initialize IList<T> C#

c#generics

提问by Anders

Is there a particular way to initialize an IList<T>? This does not seem to work:

有没有特殊的方法来初始化IList<T>?这似乎不起作用:

IList<ListItem> allFaqs = new IList<ListItem>();
// Error I get here: Cannot create an instance of the interface 'IList<ListItem>'

ReSharper suggests to initialize it like so:

ReSharper 建议像这样初始化它:

IList<ListItem> allFaqs = null;

But won't that cause a Null Reference Exception when I go to add items to this list?

但是,当我将项目添加到此列表时,这不会导致空引用异常吗?

Or should I just use List<ListItem>?

还是我应该只使用List<ListItem>

Thanks!

谢谢!

采纳答案by ChrisF

The error is because you're trying to create an instance of the interface.

错误是因为您正在尝试创建interface的实例。

Change your code to:

将您的代码更改为:

List<ListItem> allFaqs = new List<ListItem>();

or

或者

IList<ListItem> allFaqs = new List<ListItem>();

and it should compile.

它应该编译。

You'll obviously have to change the rest of your code to suit too.

您显然也必须更改其余代码以适应。

You can create any concrete type that implements IListlike this, so

你可以创建任何实现IList这样的具体类型,所以

IList<ListItem> test = new ListItem[5];

IList<ListItem> test = new ObservableCollection<ListItem>(allFaqs);

and so on, will all work.

等等,都会起作用。

回答by kemiller2002

You need to do:

你需要做:

IList<ListItem> allFaqs = new List<ListItem>();

In your code you are trying to initialize an interface.

在您的代码中,您正在尝试初始化一个接口。

回答by Michael

IList is an interface so you can't instantiate it. All sorts of classes implement IList. One such class that implements IList is List. You can instantiate it like so:

IList 是一个接口,因此您无法实例化它。各种类都实现了 IList。实现 IList 的类之​​一是 List。您可以像这样实例化它:

List<ListItem> allFaqs = new List<ListItem>();

回答by Justin Niessner

Your problem is that IList is an interface, not a class. You can't initialize an interface.

你的问题是 IList 是一个接口,而不是一个类。您无法初始化接口。

You can have an instance of an interface, but you need to initialize it with a class that implements that interface such as:

您可以拥有一个接口的实例,但您需要使用实现该接口的类对其进行初始化,例如:

IList<string> strings = new List<string>();

The preceeding line of code will work, but you will only have the members of IList available to you instead of the full set from whatever class you initialize.

前面的代码行将起作用,但您只能使用 IList 的成员,而不是您初始化的任何类的完整集合。

回答by Joseph

You can't instantiate an interface (IList is an interface).

你不能实例化一个接口(IList 是一个接口)。

You would need to instantiate something that implements an IList,

你需要实例化一些实现 IList 的东西,

like so:

像这样:

IList<ListItem> allFaqs = new List<ListItem>();

There are other kinds of ILists, but List is a good one to start with.

还有其他类型的 IList,但 List 是一个很好的开始。

回答by adrianbanks

IList<T>is an interface. You need to instantiate a class that implements the interface:

IList<T>是一个接口。您需要实例化一个实现该接口的类:

IList<ListItem> allFaqs = new List<ListItem>();

List<T>implements IList<T>, and so can be assigned to the variable. There are also other types that also implement IList<T>.

List<T>实现IList<T>,因此可以分配给变量。还有其他类型也实现了IList<T>.

回答by Michael Stum

IList is an Interface, not a class. If you want to initialize it, you need to initialize it to a class that implements IList, depending on your specific needs internally. Usually, IList is initialized with a List.

IList 是一个接口,而不是一个类。如果要对其进行初始化,则需要将其初始化为实现 IList 的类,具体取决于您内部的具体需求。通常,IList 是用一个 List 初始化的。

IList<ListItem> items = new List<ListItem>();

回答by Amy

IList is not a class; it's an interface that classes can implement. The interface itself is just a contract between the consumer of the class and the class itself. This line of code will work:

IList 不是一个类;它是一个类可以实现的接口。接口本身只是类的消费者和类本身之间的契约。这行代码将起作用:

IList<ListItem> allFaqs = new List<ListItem>();

Does this help?

这有帮助吗?