How to print a dictionary values with foreach loop in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static Dictionary<string, int> dic = new Dictionary<string, int>();

        static void Main(string[] args)
        {
            dic.Add("c", 1);
            dic.Add("c#", 2);
            dic.Add("php", 3);
            dic.Add("java", 4);

            foreach (var pair in dic) {
                Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
            }
        }
    }
}


/*
run:
  
c : 1
c# : 2
php : 3
java : 4
 
*/

 



answered Aug 15, 2018 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static Dictionary<string, int> dic = new Dictionary<string, int>();

        static void Main(string[] args)
        {
            dic.Add("c", 1);
            dic.Add("c#", 2);
            dic.Add("php", 3);
            dic.Add("java", 4);

            foreach (KeyValuePair<string, int> pair in dic) {
                Console.WriteLine("{0},{1}", pair.Key, pair.Value);
            }
        }
    }
}


/*
run:
  
c : 1
c# : 2
php : 3
java : 4
 
*/

 



answered Aug 15, 2018 by avibootz

Related questions

1 answer 192 views
2 answers 211 views
1 answer 185 views
2 answers 250 views
1 answer 225 views
2 answers 257 views
1 answer 148 views
148 views asked Mar 6, 2023 by avibootz
...