joshua key
joshua key

Reputation: 29

How to extract Arrays from List in C#?

I have a List of arrays like this

private List<Array> _data = new List<Array>();

I want to add single array as single element of list Like this..

while(someCondition)
{
    string[] BioData = new string[3];
    BioData[0] = name;
    BioData[1] = age;
    BioData[2] = location;
    _data.Add(BioData);
}

The problem is how to retrieve arrays from that list.. Here is pseudo code what I want to do..

private void LoadData()
{
    string[] data= new string[3]; 
    foreach(var data[] in _data)
    {
        name= data[0];
        age= data[1];
        location= data[2];
    }
}

But it's not working. Kindly guide me how to achieve it. Thank You

Upvotes: 0

Views: 630

Answers (1)

Diogo Rocha
Diogo Rocha

Reputation: 10575

I don't think you want the Array class, just use string[] instead.

So it would be like:

private List<string[]> _data = new List<string[]>();

...

foreach (var data in _data)
{
    name = data[0];
    age = data[1];
    location = data[2];
}

You can test it here. Please note the output and some code changes.

Upvotes: 2

Related Questions