7

How can I define two dimensional dynamic array? If I want to use List<>, can I use it for two dimensions?

1
  • 1
    please tell us your situation in more details. There are some situation when you don't need a two dimensional array, but a Dictionary<T, U> or something similar. Commented Dec 21, 2009 at 10:32

6 Answers 6

13

There's no built-in dynamic equivalent of two-dimensional arrays that I'm aware of, but you can easily get at more or less the same functionaltiy.

Define a Coordinate class with this API:

public class Coordinate : IEquatable<Coordinate>
{
     public Coordinate(int x, int y);
     public int X { get; }
     public int Y { get; }
     // override Equals and GetHashcode...
}

You can now create a collection of these Coordinate instances.

If you create a HashSet<Coordinate> you will be guaranteed that you cannot add a Coordinate if it's already added because it overrides Equals.

If you want, you can expand Coordinate to Coordinate<T> like this:

public class Coordinate<T> //...
{
    // previous stuff...

    public T Item { get; set; }
}

This will allow you to associate a strongly typed item with each coordinate, like this:

var items = new HashSet<Coordinate<string>>();
items.Add(new Coordinate<string>(1, 4) { Item = "Foo" });
items.Add(new Coordinate<string>(7, 19) { Item = "Foo" });
// ...
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this solution and I don't know if its because things have changed in newer versions of c# but these are the errors I get. 'Coordinate' Does not implement interface member'IEquatable<Coordinate>.Equals(Coordinate)' as well 'Coordinate.Coordinate(int,int)' must declare a body because it is not marked abstract, extern, or partial along with using the generic type 'Coordinate<>' requires 1 type arguments
3

you can use list e.g. List> and list in c# is very fast.

but by two-d array you need something like:

int[,] arr = new int[100,100];

and this should be the more faster than list.

Comments

2

This would be something like this:

List<List<string>> twoDimensional = new List<List<string>>();

But I think it is impractical to use a List for this, better resort to arrays...

3 Comments

That is simply a list of lists. Not 2 dimensional.
-1 This is equivalent to a jagged array, not a two-dimensioal array.
Hmm you're right.... we should have more information on the context of the problem, why he needs it etc.
2

With arrays you cant, they are never 'dynamic'.

With lists you cant either (unless you make it look like one by providing an additional indexer), unless you are simply looking for a list of lists (which is not multi-dimensional).

That would be (for a list of list of string):

List<List<string>>

Comments

1

You should take a look here: Arrays Tutorial in C#. Anyway here is the syntax: type[,] name for multidimensional arrays, type[][] name for jagged arrays (change type for the type of the objects to be stored in the array).

1 Comment

How do you add to the array after it has been declared?
0
Dictionary<T, List<T>>

for example, if T was "string" you can use it like this:

var pathes = new Dictionary<string, List<string>>();

pathes.Add("X", new List<string>());
pathes["X"].Add("a");
pathes["X"].Add("b");
pathes["X"].Add("c");

pathes.Add("Y", new List<string>());
pathes["Y"].Add("b");
pathes["Y"].Add("z");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.