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