Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a List like List<int,string>?

Tags:

c#

.net

I need a two column list like:

List<int,string> mylist= new List<int,string>(); 

it says

using the generic type System.collection.generic.List<T> requires 1 type arguments.

like image 961
Benny Ae Avatar asked Oct 07 '13 17:10

Benny Ae


People also ask

How do I make a list of strings?

To create a list of strings, first use square brackets [ and ] to create a list. Then place the list items inside the brackets separated by commas. Remember that strings must be surrounded by quotes. Also remember to use = to store the list in a variable.

How do I return a string from a list in C#?

In C# you can simply return List<string> , but you may want to return IEnumerable<string> instead as it allows for lazy evaluation. @Marc: Certainly, but if you return List<string> you don't have the option even with a lazy source.


1 Answers

Depending on your needs, you have a few options here.

If you don't need to do key/value lookups and want to stick with a List<>, you can make use of Tuple<int, string>:

List<Tuple<int, string>> mylist = new List<Tuple<int, string>>();  // add an item mylist.Add(new Tuple<int, string>(someInt, someString)); 

If you do want key/value lookups, you could move towards a Dictionary<int, string>:

Dictionary<int, string> mydict = new Dictionary<int, string>();  // add an item mydict.Add(someInt, someString); 
like image 173
newfurniturey Avatar answered Nov 07 '22 04:11

newfurniturey