I have some data in a table, but instead of using something like
string[,] myTable = new string[3, 2];
I wanted to use List, because my table can grow or shrink and I cannot do that easily with arrays.
So I got something like
List<List<string>> myTable = new List<List<double>>(3)(2);
But this gives me a "Method name expected" error. Also tried new List<List<double>(2)>(3);

I also don't want the overhead of a DataGrid in this case.
Can anyone please help or is what I'm trying to do not possible?
Thanks in advance. :)

Well first, you can't declare a List<List<string>> as a List<List<double>> as the types aren't implicitly compatible :)

List initialisation by constructor is different to arrays. You have to remember that when declaring a List you're using a generic type; List<T>()

In this case your T is List<string> a declared type (not an instance). So you're simply saying "This list will contain lists of strings"

As you're using Lists, there's no real need to declare an initial size unless you want to reserve a large amount of memory. Additionally, you will need to declare the size of each subsequent List<string> you instantiate. (As currently the container list is empty)

Now when you say your data table will shring/grow, do you mean by row or by column? A list of lists might not be what you need.


    [0]{ "Hello" }
    [1]{ "Goodbye" }
    [0]{ "Are" }
    [1]{ "You" }
    [2]{ "Well" }

@Ketsuekiame Thnks for your interesting reply. That double thing was a typo.
What I want is to have changeable arrays, a bit like ReDim in VB.
I would like to be able, to add or delete rows and columns.
According to your explanation I see two possibilities now (correct me if I'm wrong)

List<List<string>>  myTable = new List<List<string>>(); 
myTable[1][3]="example1"; //after init of the "rows" and indexer

List<string[]>  myTable = new List<string[]>(); 
myTable[1][3] ="example2";//after init of the "rows" and indexer

Lists aren't pre-defined. You have to add and remove from them (unless you give themn an initial size).

So if you do;

List<int> myList = new List<int>();
myList[3] = 12;

This will fail. Having an empty list is a bit like having an array of 0 length.

To actually have index 3 set to 12, you would need to do

// this inserts three entries into the list
List<int> myList = new List<int>() {0, 0, 0, 12};

When you initialise an array to a particular size, you're actually pre-filling it with the default for that object.


int[] myInts = new int[4]();

Could be considered equivalent to;

// The 4 in the constructor tells C# my initial list size will be 4 objects. It isn't necessary.
List<int> myInts = new List<int>(4) { default(int), default(int), default(int), default(int) };

To answer your original question; a List<List<T>> will do what you want. But remember that you can't set arbitrary locations. If you want to do that, you may need ArrayList or to redefine your arrays and copy the data across.

A better design, in my opinion. Would be to use proper objects.

public class MyDataColumn
    public int Index

    public string ShortName

    pubic Type DataType


public class MyDataItem
    public int RowNumber

    public MyDataColumn AssociatedColumn

    public object Data


List<MyDataItem> dataItems;


It's harder to set up the databinding and there's a bit more to manage but that should work.

commented: Great knowledge! +15

Thanks again Dan for your answer. It brought up the idea of using a DataTable! Why reinvent the wheel, as what I need(I think that is) is already in .NET!
Another idea I had, was using just a 1D List and an indexer to access the different "rows" and "columns", after filling the List with Add:

public class table
        List<string> tbl;
        int nCols;
        int nRows;

        public table(int rows, int cols)
            nCols = cols;
            nRows = rows;
            tbl = new List<string>();

        public string this[int R, int C]
            get { return tbl[R * nRows + C]; }

        //more here

But I think I'll go for the DataTable. Ha! Those design decesions!
Awaiting your unscrupulous comments :)