Accessing Elements from Multi Dimensional Array in C#

Riffy 276 Reputation points
2022-03-17T21:40:55.283+00:00

Hi

I have created a Multi Dimensional Array as follows:

            List<(int, string, string, int, int, int)> QuizList = new List<(int, string, string, int, int, int)>();
            int ItemNo = 0;
            QuizList.Add((++ItemNo, "Quiz Ans 1", "Quiz Meaning 1", 1, 3, 5));
            QuizList.Add((++ItemNo, "Quiz Ans 2", "Quiz Meaning 2", 4, 2, 1));
            QuizList.Add((++ItemNo, "Quiz Ans 3", "Quiz Meaning 3", 3, 2, 4));
            QuizList.Add((++ItemNo, "Quiz Ans 4", "Quiz Meaning 4", 4, 1, 5));
            int Item1 = 1;
            int Item2 = 2;
            string ArrayString = QuizList[Item1, Item2];
            Item1 = 3;
            Item2 = 4;
            int ArrayNo = QuizList[Item1, Item2];

I would like to access various elements from the Array, but the use of ArrayString and ArrayNo are failing in my C# cde.

This may seem a very basic issue, but am struggling to make it work.

So any solutions would be welcome.

Thanks

Developer technologies | C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-03-18T09:44:48.323+00:00

    @Riffy , Welcome to Microsoft Q&A, based on my test, you could access the various elements from the Array like the following:

        int Item1 = 1;  
        int Item2 = 2;  
        string ArrayString = QuizList[Item1-1].Item2;  
        Item1 = 3;  
        Item2 = 4;  
        int ArrayNo = QuizList[Item1-1].Item4;  
    

    Best regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-03-17T22:01:25.367+00:00

    The syntax is...

    List<(int, string, string, int, int, int)> QuizList = new List<(int, string, string, int, int, int)>();  
    int ItemNo = 0;  
    QuizList.Add((++ItemNo, "Quiz Ans 1", "Quiz Meaning 1", 1, 3, 5));  
    QuizList.Add((++ItemNo, "Quiz Ans 2", "Quiz Meaning 2", 4, 2, 1));  
    QuizList.Add((++ItemNo, "Quiz Ans 3", "Quiz Meaning 3", 3, 2, 4));  
    QuizList.Add((++ItemNo, "Quiz Ans 4", "Quiz Meaning 4", 4, 1, 5));  
              
    foreach(var item in QuizList)  
    {  
        Console.Write($"{item.Item1}\t");  
        Console.Write($"{item.Item2}\t");  
        Console.Write($"{item.Item3}\t");  
        Console.Write($"{item.Item4}\t");  
        Console.Write($"{item.Item5}");  
        Console.WriteLine();  
    }  
    

    I recommend creating a List<QuizItem> where QuizItem is a class with properties names rather than item1, 2, 3, etc.

    https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-6.0


  2. WayneAKing 4,931 Reputation points
    2022-03-19T18:13:06.547+00:00

    So why can I not use:

    >

    string ArrayString = QuizList[Item1, Item2];
    
    int ArrayNo = QuizList[Item1, Item2];
    

    for accessing any element in the List. I get compilation errors.

    Your terminology and attempted syntax suggests that you are
    confusing two entirely different types of collections.

    Nowhere in your code are you using an array or Array.
    You are using a List not an Array.

    You are trying to use the syntax which is used to access
    elements of a multi-dimensional array, which is not valid
    syntax for a List.

    • Wayne

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.