初始化的IList< T> C# [英] Initialize IList<T> C#

查看:123
本文介绍了初始化的IList< T> C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有初始化以特定方式的IList< T> ?这似乎不工作:

 的IList<&列表项GT; allFaqs =新的IList<&列表项GT;(); 
//错误我来到这里:无法创建接口的实例的IList<列表项>'

ReSharper的建议将其初始化像这样:

 的IList<&列表项GT; allFaqs = NULL; 



但不会导致一个空引用异常时,我去项目添加到这个列表<? / p>

或者我应该只使用列表<列表项>



谢谢!


解决方案

这个错误是因为你想创建的接口的实例



你的代码更改为:

 列表<列表项> allFaqs =新的List<&列表项GT;(); 

 的IList<&列表项GT; allFaqs =新的List<&列表项GT;(); 

和它应该编译。



您显然会必须改变你的代码,以满足过的其余部分。


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

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:

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>?

Thanks!

解决方案

The error is because you're trying to create an instance of the 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.

这篇关于初始化的IList&LT; T&GT; C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆