blueberry73
blueberry73

Reputation: 21

Reach a specific element of an array in a list

I have a list that contains string arrays with 3 elements, I need to get the value of the third element of the array that is in the list but I can't figure out the syntax and I can't find anything online on how to do it. My best guess so far has been something along the lines of:

Console.WriteLine(myList[0], myArray[2]);

So I want to specify what index of the list I want to work with, and then specify the index of the array that I want the value from.

Upvotes: 0

Views: 803

Answers (2)

suchoss
suchoss

Reputation: 3065

You can do it like this:

public static void Main()
{
    var myList = new List<string[]>(){
        new string[]{"a-first","a-second","a-third"},
        new string[]{"b-first","b-second","b-third"},
        new string[]{"c-first","c-second","c-third"},
        new string[]{"d-first","d-second","d-third"}
    };

    Console.WriteLine(myList[0][2]);
    Console.WriteLine(myList[2][2]);
}

First you are indexing in list, then in array myList[ListIndex][ArrayIndex].

It is like shortcut for following:

var stringArray = myList[1];
Console.WriteLine(stringArray[2]);

Upvotes: 1

work89
work89

Reputation: 85

Accessing that value and printing it to the console would be done like this:

Console.WriteLine(myList[0][2]);

This assumes the array you're accessing is the first one in the List object.

Upvotes: 0

Related Questions