Anders
Anders

Reputation: 12560

Initialize IList<T> C#

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!

Upvotes: 27

Views: 96306

Answers (8)

ChrisF
ChrisF

Reputation: 137148

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.

Upvotes: 60

user47589
user47589

Reputation:

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?

Upvotes: 6

Michael Stum
Michael Stum

Reputation: 180944

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>();

Upvotes: 2

adrianbanks
adrianbanks

Reputation: 82944

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

Upvotes: 1

Joseph
Joseph

Reputation: 25513

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.

Upvotes: 2

Justin Niessner
Justin Niessner

Reputation: 245429

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.

Upvotes: 21

Michael
Michael

Reputation: 55415

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>();

Upvotes: 2

kemiller2002
kemiller2002

Reputation: 115488

You need to do:

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

In your code you are trying to initialize an interface.

Upvotes: 3

Related Questions