Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like List<String, Int32, Int32> (multidimensional generic list)

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

like image 345
Alex Avatar asked Jun 08 '10 04:06

Alex


3 Answers

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

like image 24
Pavel Belousov Avatar answered Oct 08 '22 08:10

Pavel Belousov


Well, you can't do this til C# 3.0, use Tuples if you can use C# 4.0 as mentioned in other answers.

However In C# 3.0 - create an Immutable structure and wrap all types' insances within the structure and pass the structure type as generic type argument to 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 create immutable structs - checkout here.

like image 29
this. __curious_geek Avatar answered Oct 08 '22 10:10

this. __curious_geek


Best way is to create a container for it ie a 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>();
like image 52
Jason Jong Avatar answered Oct 08 '22 08:10

Jason Jong