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...
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> .
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.
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>>();
try this
IList<IList<string>> matrix = new List<IList<string>>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With