How to use dictionary in C#

6 Answers

0 votes
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        Dictionary<string, int> dic = new Dictionary<string, int>();  
         
        dic.Add("c#", 1);
        dic.Add("c", 2);
        dic.Add("c++", 3);
        dic.Add("java", 4);
        dic.Add("php", 5);
         
        foreach(KeyValuePair<string, int> item in dic) { 
              Console.WriteLine("{0} : {1}", item.Key, item.Value); 
        } 
    }
}
 
 
 
/*
run:
 
c# : 1
c : 2
c++ : 3
java : 4
php : 5
 
*/

 



answered Sep 5, 2019 by avibootz
edited Sep 5, 2019 by avibootz
0 votes
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        Dictionary<string, int> dic = new Dictionary<string, int>() { 
                                                                {"c#", 1}, 
                                                                {"c", 2}, 
                                                                {"java", 3} 
                                                            };  
         
 
        foreach(KeyValuePair<string, int> item in dic) { 
              Console.WriteLine(item.Key + " " + item.Value); 
        } 
    }
}
 
 
 
/*
run:
 
c# 1
c 2
java 3
 
*/

 



answered Sep 5, 2019 by avibootz
edited Sep 5, 2019 by avibootz
0 votes
using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    static void Main() {
        Dictionary<string, int> dic = new Dictionary<string, int>() { 
                                                                {"c#", 1}, 
                                                                {"c", 2}, 
                                                                {"java", 3} 
                                                            };  
         
        var list = dic.Keys.ToList();
 
        foreach (var key in list) {
                Console.WriteLine("{0} : {1}", key, dic[key]);
        }
    }
}
 
 
 
/*
run:
 
c# : 1
c : 2
java : 3
 
*/

 



answered Sep 5, 2019 by avibootz
0 votes
using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    static void Main() {
        Dictionary<string, int> dic = new Dictionary<string, int>() { 
                                                                {"c#", 1}, 
                                                                {"c", 2}, 
                                                                {"java", 3} 
                                                            };  
         
        var lst = dic.ToList();

        for (int i = 0; i < lst.Count; i++)
            Console.WriteLine("{0} {1}", lst[i].Key, lst[i].Value);
        }
}
 
 
 
/*
run:
 
c# 1
c 2
java 3
 
*/

 



answered Sep 5, 2019 by avibootz
0 votes
using System;
using System.Collections.Generic;
  
class Program
{
    static void Main() {
        Dictionary<string, int> dic = new Dictionary<string, int>();  
          
        dic.Add("c#", 1);
        dic.Add("c", 2);
        dic.Add("c++", 3);
        dic.Add("java", 4);
        dic.Add("php", 5);
          
        foreach(KeyValuePair<string, int> item in dic) { 
              Console.WriteLine("{0} : {1}", item.Key, item.Value); 
        } 
        
        Console.WriteLine("\n");
        
        dic.Remove("java");
        
        foreach(KeyValuePair<string, int> item in dic) { 
              Console.WriteLine("{0} : {1}", item.Key, item.Value); 
        }
    }
}
  
  
  
/*
run:
  
c# : 1
c : 2
c++ : 3
java : 4
php : 5


c# : 1
c : 2
c++ : 3
php : 5

*/

 



answered Sep 12, 2019 by avibootz
0 votes
using System;
using System.Collections.Generic;
  
class Program
{
    static void Main() {
        Dictionary<string, int> dic = new Dictionary<string, int>();  
          
        dic.Add("c#", 1);
        dic.Add("c", 2);
        dic.Add("c++", 3);
        dic.Add("java", 4);
        dic.Add("php", 5);
          
        foreach(KeyValuePair<string, int> item in dic) { 
              Console.WriteLine("{0} : {1}", item.Key, item.Value); 
        } 
        
        Console.WriteLine("\n");
        
        dic.Clear();
        
        foreach(KeyValuePair<string, int> item in dic) { 
              Console.WriteLine("{0} : {1}", item.Key, item.Value); 
        }
    }
}
  
  
  
/*
run:
  
c# : 1
c : 2
c++ : 3
java : 4
php : 5

*/

 



answered Sep 12, 2019 by avibootz

Related questions

1 answer 205 views
205 views asked Nov 26, 2020 by avibootz
1 answer 240 views
1 answer 257 views
1 answer 238 views
238 views asked Jan 5, 2017 by avibootz
1 answer 264 views
264 views asked Jan 5, 2017 by avibootz
2 answers 334 views
...