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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to access a Dictionary with two-dimensional arrays in C#

1 Answer

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

class Program
{
    static void Main()
    {
        // Define the dictionary
        Dictionary<string, int[,]> dict = new Dictionary<string, int[,]>();

        // Add values to the dictionary
        dict["matrix1"] = new int[,] { { 1, 2 }, { 3, 4 } };
        dict["matrix2"] = new int[,] { { 5, 6 }, { 7, 8 } };

        // Access values
        int[,] matrix1 = dict["matrix1"];
        int[,] matrix2 = dict["matrix2"];

        // Print values of matrix1
        for (int i = 0; i < matrix1.GetLength(0); i++) {
            for (int j = 0; j < matrix1.GetLength(1); j++) {
                Console.Write(matrix1[i, j] + " ");
            }
            Console.WriteLine();
        }

        // Access a specific element
        int value = dict["matrix2"][1, 1]; 
        Console.WriteLine("Element at [1,1] in matrix2 = " + value);
    }
}



/*
run:

1 2 
3 4 
Element at [1,1] in matrix2 = 8

*/

 



answered Jan 21, 2025 by avibootz

Related questions

...