There is something like List <String, Int32, Int32> (multi-dimensional generic list)

I need something similar to List<String, Int32, Int32> . A list supports only one type at a time, and a dictionary only supports two. Is there a clean way to do something like the above (multidimensional generic list / collection)?

+7
collections list c # generic-list
source share
4 answers

The best way is to create a container for it, i.e. the class

 public class Container { public int int1 { get; set; } public int int2 { get; set; } public string string1 { get; set; } } 

then in the code where you need it

 List<Container> myContainer = new List<Container>(); 
+14
source share

In .NET 4, you can use List<Tuple<String, Int32, Int32>> .

+13
source share

Well, you cannot do this with C # 3.0, use Tuples if you can use C # 4.0 as mentioned in other answers.

However, in C # 3.0 - create Immutable structure objects and wrap all types in a structure and pass the structure type as an argument to the type of the general type in your list.

 public struct Container { public string String1 { get; private set; } public int Int1 { get; private set; } public int Int2 { get; private set; } public Container(string string1, int int1, int int2) : this() { this.String1 = string1; this.Int1 = int1; this.Int2 = int2; } } //Client code IList<Container> myList = new List<Container>(); myList.Add(new Container("hello world", 10, 12)); 

If you're curious why creating immutable structures is here, here .

+1
source share

Based on your comment, it seems like you need a structure with two integers that are stored in a dictionary with a string key.

 struct MyStruct { int MyFirstInt; int MySecondInt; } ... Dictionary<string, MyStruct> dictionary = ... 
0
source share

All Articles