How to iterate over a collection of key-value pairs (associative array) in C#

5 Answers

0 votes
// Iterate Over Key–Value Pairs
// Most common pattern

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        var dict = new Dictionary<string, int>
        {
            ["Alice"] = 10,
            ["Bob"] = 17,
            ["Marley"] = 23,
            ["Charlie"] = 36
        };

        foreach (var kvp in dict) {
            Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
        }
    }
}


/*
run:

Key: Alice, Value: 10
Key: Bob, Value: 17
Key: Marley, Value: 23
Key: Charlie, Value: 36

*/

 



answered Mar 22 by avibootz
0 votes
// Iterate Over Keys Only

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        var dict = new Dictionary<string, int>
        {
            ["Alice"] = 10,
            ["Bob"] = 17,
            ["Marley"] = 23,
            ["Charlie"] = 36
        };

        foreach (var key in dict.Keys) {
            Console.WriteLine($"Key: {key}");
        }
    }
}


/*
run:

Key: Alice
Key: Bob
Key: Marley
Key: Charlie

*/

 



answered Mar 22 by avibootz
0 votes
// Iterate Over Values Only

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        var dict = new Dictionary<string, int>
        {
            ["Alice"] = 10,
            ["Bob"] = 17,
            ["Marley"] = 23,
            ["Charlie"] = 36
        };

        foreach (var value in dict.Values) {
            Console.WriteLine($"Value: {value}");
        }
    }
}


/*
run:

Value: 10
Value: 17
Value: 23
Value: 36

*/

 



answered Mar 22 by avibootz
0 votes
// Using foreach With Deconstruction

/*
In a foreach loop, unpack each pair into k and v variables. 
To deconstruct classes, we have to implement the
Deconstruct method. string Variable { get; set; }
*/

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        var dict = new Dictionary<string, int>
        {
            ["Alice"] = 10,
            ["Bob"] = 17,
            ["Marley"] = 23,
            ["Charlie"] = 36
        };

        foreach (var (key, value) in dict) {
            Console.WriteLine($"{key} = {value}");
        }
    }
}



/*
run:

Alice = 10
Bob = 17
Marley = 23
Charlie = 36

*/

 



answered Mar 22 by avibootz
0 votes
// LINQ Options (When Filtering)

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        var dict = new Dictionary<string, int>
        {
            ["Alice"] = 10,
            ["Bob"] = 17,
            ["Marley"] = 23,
            ["Charlie"] = 36
        };

        foreach (var (key, value) in dict.Where(kvp => kvp.Value > 21)) {
            Console.WriteLine($"{key}: {value}");
        }
    }
}


/*
run:

Marley: 23
Charlie: 36

*/

 



answered Mar 22 by avibootz

Related questions

...