Sphax
Sphax

Reputation: 95

Declaring a 2D array without knowing its size in C#

I would like to use a 2-D array but I can't know its size in advance. So my question is: how to declare it? And then how to add values into it?

String[,] tabConfig = new String[?, 4];
foreach(blabla with i)
{
    tabConfig[i, 0] = a;
    tabConfig[i, 1] = b;
    tabConfig[i, 2] = c;
    tabConfig[i, 3] = d;
}

I know I can also use a list but I am not very familiar with it.

Thank you!

EDIT: Brace yourselves! Here come my true code with Jon Skeet's help!

List<string[]> tabConfig = new List<string[]>();
String[] temp = new String[4];//The array that will be inside the List
int line = 0, column = 0;

foreach (XmlNode e in doc.DocumentElement.ChildNodes)
{
    if (e.Attributes["Server"].Value == choice)
    {
        temp[0] = e.Attributes["Serveur"].Value;//Here is value 'a'

        column = 1;
        foreach (XmlNode i in e.ChildNodes)
        {
            temp[colonne] = i.InnerText;//Here are values 'b', 'c' and 'd'
            column++;
        }
        tabConfig.Add(temp);//Put a new line into the List
        line++;
    }
}

And to call it:

foreach(string[] array in tabConfig)
    foreach(String txt in array)
        Console.WriteLine(txt);

Upvotes: 6

Views: 11571

Answers (7)

Kane
Kane

Reputation: 16812

You are probably better of using a dictionary something like

IDictionary<string, IList<TYPE>> myContainer = new Dictionary<string, List<TYPE>>();
myContainer.Add("key1", new List<TYPE>());
myContainer["key1"].Add("SomeTYPE-1");
myContainer["key1"].Add("SomeTYPE-2");
myContainer["key1"].Add("SomeTYPE-3");

myContainer.Add("key2", new List<TYPE>());
myContainer["key2"].Add("A");
myContainer["key2"].Add("B");
myContainer["key2"].Add("C");

Update As commented by @jon you use a dictionary if you are wanting to access elements of your 2d array by some form of key, which may / may not be useful in your situation. If you don't want a key based dictionary then stick with what @jon posted.

Upvotes: 2

spaghettifunk
spaghettifunk

Reputation: 2014

if I were you, I'd put as array-size the length of the list you are iterating because array must be declared with a fixed size

Upvotes: 1

A.K.
A.K.

Reputation: 3331

Make a class like this

public class MyClass
{
public string one{get;set;}
public string two{get;set;}
}

Now you can make a list of this object like

List<MyClass> list1=new List<MyClass>();

Upvotes: 1

Nicke Manarin
Nicke Manarin

Reputation: 3360

Workaround:

private struct MyStruct
{
    int x;
    int y;
}

List<MyStruct> myList = new List<MyStruct>(); 

Upvotes: 1

Paulo Correia
Paulo Correia

Reputation: 616

If you can do a foreach, you can know the size of it. If you know the size, you can create the aray with the necessary size.

But I would use a List for it. It's friendlier for that type of situation.

http://www.functionx.com/csharp/builtinclasses/list.htm

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503280

So my question is: how to declare it?

You can't. All arrays in .NET are fixed size: you can't create an array instance without knowing the size.

It looks to me like you should have a List<T> - and I'd actually create a class to hold the four properties, rather than just using four elements of an array, probably. You could use a list of arrays:

List<string[]> tabConfig = new List<string[]>();
foreach (...)
{
    tabConfig.Add(new string[] { a, b, c, d });
}

... but then you need to know what those four elements mean in terms of ordering etc, which is likely to make the rest of your code harder to understand.

Upvotes: 15

Oscar
Oscar

Reputation: 13990

You can use a

List<List<string>> 

for this, dont' you?

Upvotes: 3

Related Questions