Monday, 8 May 2017

List Example with Keyvaluepair

KeyValuePair

var myList = new List<KeyValuePair<int, string>>();
myList.Add(new KeyValuePair<int, string>(1, "One");

foreach (var item in myList)
{
    int i = item.Key;
    string s = item.Value;
}
or if you are .NET Framework 4 you could use:

var myList = new List<Tuple<int, string>>();
myList.Add(Tuple.Create(1, "One"));

foreach (var item in myList)
{
    int i = item.Item1;
    string s = item.Item2;
}
If either the string or integer is going to be unique to the set, you could use:

Dictionary<int, string> or Dictionary<string, int>