Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to create a dictionary in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;
   
class Program
{
    static void Main() {
        IDictionary<int, string> dict = new Dictionary<int, string>();
        
        dict.Add(1, "c#"); 
        dict.Add(15, "vb.net");
        dict.Add(7, "java");
        dict.Add(2, "python");

        foreach(KeyValuePair<int, string> keyval in dict)
            Console.WriteLine("Key: {0} Value: {1}", keyval.Key, keyval.Value);
    }
}
   
   
   
   
/*
run:
   
Key: 1 Value: c#
Key: 15 Value: vb.net
Key: 7 Value: java
Key: 2 Value: python
   
*/

 





answered Jan 2, 2022 by avibootz
0 votes
using System;
using System.Collections.Generic;
   
class Program
{
    static void Main() {
        
        var dict = new Dictionary<int, string>() { {1, "c#"},
	                                               {15, "vb.net"},
	                                               {7, "java"},
	                                               {2, "python"}
        };
		
        foreach(var keyval in dict)
               Console.WriteLine("Key: {0} Value: {1}", keyval.Key, keyval.Value);
    }
}
   
   
   
   
/*
run:
   
Key: 1 Value: c#
Key: 15 Value: vb.net
Key: 7 Value: java
Key: 2 Value: python
   
*/

 





answered Jan 2, 2022 by avibootz

Related questions

2 answers 28 views
1 answer 32 views
32 views asked Dec 10, 2022 by avibootz
1 answer 62 views
1 answer 382 views
2 answers 97 views
97 views asked Feb 10, 2017 by avibootz
...