How to iterate over map keys and values in C#

1 Answer

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

class Program
{
    static void Main() {
        var map = new Dictionary<string, int> {
            ["c"] = 1,
            ["cpp"] = 2,
            ["java"] = 3,
            ["csharp"] = 4
        };

        foreach(var entry in map) {
            Console.WriteLine("Key:" + entry.Key + " Value:" + entry.Value);
        }
    }
}




/*
run:

Key:c Value:1
Key:cpp Value:2
Key:java Value:3
Key:csharp Value:4

*/

 



answered Feb 1, 2023 by avibootz

Related questions

1 answer 64 views
64 views asked Aug 6, 2025 by avibootz
3 answers 215 views
1 answer 113 views
3 answers 160 views
1 answer 97 views
3 answers 160 views
1 answer 107 views
...