代码之家  ›  专栏  ›  技术社区  ›  Alex

是否有类似List<String,Int32,Int32>(多维泛型列表)的东西

  •  7
  • Alex  · 技术社区  · 15 年前

    我需要类似的东西 List<String, Int32, Int32>

    4 回复  |  直到 15 年前
        1
  •  14
  •   Jason Jong    15 年前

    最好的方法是为它创建一个容器,即类

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

    List<Container> myContainer = new List<Container>();
    
        2
  •  13
  •   Pavel Belousov    15 年前

    在.NET4中,您可以使用 List<Tuple<String, Int32, Int32>>

        3
  •  1
  •   Community CDub    8 年前

    好吧,在C#3.0之前你不能这么做,如果你能用C#4.0的话就用元组,就像其他答案中提到的那样。

    但是在C#3.0中-创建 Immutable structure

    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));
    

    如果你好奇为什么要创建不可变的结构- checkout here .

        4
  •  0
  •   Austin Salonen gmlacrosse    15 年前

    根据您的评论,听起来您需要一个结构,它包含两个整数,并用字符串键存储在字典中。

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