How to initialize dictionary in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        var dict = new Dictionary<string, int> { ["c#"] = 10, 
                                                 ["c++"] = 20,
                                                 ["rust"] = 35,
                                                 ["dart"] = 37,
                                                 ["java"] = 43 };
         
        foreach (var pair in dict) {
            Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
        }
    }
}
 
 
 
 
 
/*
run:
 
c# : 10
c++ : 20
rust : 35
dart : 37
java : 43
 
*/

 



answered Nov 26, 2020 by avibootz
edited Jan 13, 2023 by avibootz
0 votes
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        var dict = new Dictionary<string, int> { {"c#", 10}, 
                                                 {"c++", 20},
                                                 {"rust", 35},
                                                 {"python", 37},
                                                 {"java", 43} };
                                                 
        foreach (var pair in dict) {
            Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
        }
    }
}
 
 
 
 
 
/*
run:
 
c# : 10
c++ : 20
rust : 35
python : 37
java : 43
 
*/

 



answered Jan 13, 2023 by avibootz

Related questions

2 answers 119 views
1 answer 149 views
149 views asked Aug 24, 2018 by avibootz
1 answer 157 views
1 answer 143 views
3 answers 156 views
1 answer 147 views
147 views asked Sep 19, 2020 by avibootz
2 answers 230 views
...