How to iterate over a dictionary in C#

2 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(var item in dic) {
            Console.WriteLine("{0} = {1}", item.Key, item.Value);
        }
    }
}
  
  
  
  
  
/*
run:
  
c# = 1
c = 2
c++ = 3
java = 4
php = 5
  
*/

 



answered Jun 16, 2021 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);
        dic.Add("python", 6);
          
        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
python : 6
  
*/

 



answered Jun 16, 2021 by avibootz

Related questions

...