Alex Rivera | Logout

Initialize IList<T> C#

Asked 2009-08-28T19:54:45.110
27

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!

Edit
Report

1 Answer

6

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<ListItem> allFaqs = new List<ListItem>();

Does this help?

answered 2009-08-28T19:58:04.887

Your Answer