Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initialize a IList<IList<string>>?

Tried with :

IList<IList<string>> matrix = new List<new List<string>()>();

but I can't. How can I do it? I need a matrix of strings...

like image 238
markzzz Avatar asked Feb 06 '12 10:02

markzzz


People also ask

How do I declare an IList?

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

What IList in c#?

In C# IList interface is an interface that belongs to the collection module where we can access each element by index. Or we can say that it is a collection of objects that are used to access each element individually with the help of an index. It is of both generic and non-generic types.


2 Answers

You'd need:

IList<IList<string>> matrix = new List<IList<string>>();

but then you can happen to always add a List<string> for each element.

The reason this won't work:

// Invalid
IList<IList<string>> matrix = new List<List<string>>();

is that it would then be reasonable to write:

// string[] implements IList<string>
matrix.Add(new string[10]);

... but that would violate the fact that the list is really a List<List<string>> - it's got to contain List<string> values, not just any IList<string>... whereas my declaration at the top just creates a List<IList<string>>, so you could add a string array to it without breaking type safety.

Of course, you could change to use the concrete type in your declaration instead:

IList<List<string>> matrix = new List<List<string>>();

or even:

List<List<string>> matrix = new List<List<string>>();
like image 53
Jon Skeet Avatar answered Nov 15 '22 06:11

Jon Skeet


try this

    IList<IList<string>> matrix = new List<IList<string>>();
like image 34
Orkun Ozen Avatar answered Nov 15 '22 05:11

Orkun Ozen