How to add key value pair in a List with C#

2 Answers

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<KeyValuePair<string, int>>();

            list.Add(new KeyValuePair<string, int>("c#", 1));
            list.Add(new KeyValuePair<string, int>("java", 2));
            list.Add(new KeyValuePair<string, int>("c++", 3));
            list.Add(new KeyValuePair<string, int>("c", 4));

            foreach (var e in list)
            {
                Console.WriteLine(e);
            }
        }
    }
}

/*
run:
 
[c#, 1]
[java, 2]
[c++, 3]
[c, 4]
 
*/

 



answered Jan 5, 2017 by avibootz
edited Jan 5, 2017 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<KeyValuePair<string, int>>();

            list.Add(new KeyValuePair<string, int>("c#", 1));
            list.Add(new KeyValuePair<string, int>("java", 2));
            list.Add(new KeyValuePair<string, int>("c++", 3));
            list.Add(new KeyValuePair<string, int>("c", 4));

            foreach (var e in list)
            {
                Console.WriteLine(e.Key + " - " + e.Value);
            }
        }
    }
}

/*
run:

c# - 1
java - 2
c++ - 3
c - 4

*/


 



answered Jan 5, 2017 by avibootz
edited Jan 5, 2017 by avibootz

Related questions

1 answer 150 views
1 answer 111 views
2 answers 214 views
1 answer 113 views
1 answer 119 views
1 answer 137 views
1 answer 142 views
...