25

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!

8 Answers 8

58

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.

2
  • What if I want to use IList as most generic type for both arrays and lists? Dec 11, 2018 at 11:52
  • @EduardoPignatelli you can use anything that implements IList. I've added a couple of examples.
    – ChrisF
    Dec 11, 2018 at 12:25
19

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

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.

0
5

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?

2

You need to do:

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

In your code you are trying to initialize an interface.

1

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:

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

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

You would need to instantiate something that implements an IList,

like so:

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

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

1

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.