Initialize IList<T> C#

c#, generics

Solution

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.

You can create any concrete type that implements `IList` like this, so

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

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

and so on, will all work.

Problem

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!

Original source

Related problems