How to convert a dictionary to KeyValuePair list in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        var dict = new Dictionary<string, int>()
        {
            { "c#", 2 }, { "c++", 5 }, { "java", 7 }, { "c", 1 }, { "rust", 3 }
        };

        List<KeyValuePair<string, int>> list = dict.ToList();
        
        foreach (var pair in list) {
            Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
        }
    }
}
  
  
  
  
  
/*
run:
  
c# : 2
c++ : 5
java : 7
c : 1
rust : 3
  
*/

 



answered Apr 29, 2023 by avibootz

Related questions

1 answer 144 views
1 answer 113 views
1 answer 100 views
100 views asked Apr 29, 2023 by avibootz
1 answer 137 views
2 answers 259 views
259 views asked Aug 24, 2018 by avibootz
1 answer 213 views
2 answers 236 views
...