【问题标题】:define a List like List<int,string>?定义一个 List 像 List<int,string>?
【发布时间】:2013-10-14 10:00:54
【问题描述】:

我需要一个两列列表,例如:

List<int,string> mylist= new List<int,string>();

它说

使用泛型类型 System.collection.generic.List&lt;T&gt; 需要 1 个类型参数。

【问题讨论】:

  • 改用List&lt;custom class&gt;

标签: c# .net


【解决方案1】:

根据您的需要,这里有几个选项。

如果您不需要进行键/值查找并想坚持使用List&lt;&gt;,您可以使用Tuple&lt;int, string&gt;

List<Tuple<int, string>> mylist = new List<Tuple<int, string>>();

// add an item
mylist.Add(new Tuple<int, string>(someInt, someString));

如果您确实需要键/值查找,您可以转向Dictionary&lt;int, string&gt;

Dictionary<int, string> mydict = new Dictionary<int, string>();

// add an item
mydict.Add(someInt, someString);

【讨论】:

  • 有了Tuple&lt;,&gt;,写mylist.Add(Tuple.Create(someInt, someString));会容易一些。
  • @JeppeStigNielsen 这是个人喜好; Tuple.Create(t1, t2) 在功能上等同于 new Tuple&lt;t1, t2&gt;(t1, t2),我个人更喜欢写作和阅读后者,因为它更明确。我不认为这是更多的工作 - 但我喜欢确切地看到需要什么=P
  • 也有SortedDictionary&lt;int, string&gt;SortedList&lt;int, string&gt;,不过要看他的用途。
【解决方案2】:

不确定您的具体情况,但您有三个选择:

1.) 使用字典<..>
2.) 围绕您的值创建一个包装类,然后您可以使用 List
3.) 使用元组

【讨论】:

    【解决方案3】:

    由于您的示例使用通用 List,我假设您不需要对数据使用索引或唯一约束。 List 可能包含重复值。如果您想确保唯一的密钥,请考虑使用Dictionary&lt;TKey, TValue&gt;()

    var list = new List<Tuple<int,string>>();
    
    list.Add(Tuple.Create(1, "Andy"));
    list.Add(Tuple.Create(1, "John"));
    list.Add(Tuple.Create(3, "Sally"));
    
    foreach (var item in list)
    {
        Console.WriteLine(item.Item1.ToString());
        Console.WriteLine(item.Item2);
    }
    

    【讨论】:

      【解决方案4】:

      为此,您可以使用Dictionary,其中int 是键。

      new Dictionary<int, string>();
      

      如果你真的想使用一个列表,它可能是一个List&lt;Tuple&lt;int,string&gt;&gt;(),但是Tuple 类是只读的,所以你必须重新创建实例来修改它。

      【讨论】:

        【解决方案5】:

        您可以使用 不可变 结构

        public struct Data
        {
            public Data(int intValue, string strValue)
            {
                IntegerData = intValue;
                StringData = strValue;
            }
        
            public int IntegerData { get; private set; }
            public string StringData { get; private set; }
        }
        
        var list = new List<Data>();
        

        KeyValuePair&lt;int, string&gt;

        using Data = System.Collections.Generic.KeyValuePair<int, string>
        ...
        var list = new List<Data>();
        list.Add(new Data(12345, "56789"));
        

        【讨论】:

        • @weston 我很想听听你的理由,因为根据docs,这似乎是可以接受的 - The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.
        • @weston 不仅如此,在可能有很多类的情况下,它比一个类更有效,例如List&lt;T&gt;/T[]。所以在我看来,这是一个比上课更好的建议。
        • +0: 自定义对象是个好主意,但建议使用struct 而不确保人们对差异有很好的理解是危险的。 (也看到有人试图编译像list[0].StringData = "test" 这样的东西是罕见的享受)。
        • 当你传递一个结构体时,它是按值传递的,所以如果你想考虑效率,考虑一下。这也是可变结构的主要问题。很难看出它们是结构,因此如果您修改一个值,那么根据 Alexei 的示例,如果您愿意,该更改后的值只有一个本地生命周期。
        • 有些时候确实需要担心内存问题,但是您可以看到的错误是为节省的这几个字节付出的代价。
        【解决方案6】:

        使用 C# 字典数据结构对你有好处...

        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict.Add("one", 1);
        dict.Add("two", 2);
        

        您可以通过简单的方式从字典中检索数据..

        foreach (KeyValuePair<string, int> pair in dict)
        {
            MessageBox.Show(pair.Key.ToString ()+ "  -  "  + pair.Value.ToString () );
        }
        

        更多使用 C# 字典的例子...C# Dictionary

        导航。

        【讨论】:

          【解决方案7】:
          List<Tuple<string, DateTime, string>> mylist = new List<Tuple<string, DateTime,string>>();
          mylist.Add(new Tuple<string, DateTime, string>(Datei_Info.Dateiname, Datei_Info.Datum, Datei_Info.Größe));
          for (int i = 0; i < mylist.Count; i++)
          {
               Console.WriteLine(mylist[i]);
          }
          

          【讨论】:

          • 您至少应该尝试使您的答案与问题相匹配。 OP 不想要&lt;string, DateTime, string&gt; - 他们想要&lt;int, string&gt;。虽然方法是一样的,但没有必要混淆这个问题。
          【解决方案8】:

          使用来自 C# 7(VS 2017 及更高版本)的新 ValueTuple,有一个新的解决方案:

          List<(int,string)> mylist= new List<(int,string)>();
          

          这会创建一个ValueTuple 类型的列表。如果您的目标是 .NET Framework 4.7+ 或 .NET/.NET Core,则它是本机的,否则您必须获取 ValueTuple package from nuget

          这是一个与Tuple相反的结构,它是一个类。与Tuple 类相比,它还具有可以创建命名元组的优势,如下所示:

          var mylist = new List<(int myInt, string myString)>();
          

          这样你就可以像mylist[0].myIntmylist[0].myString一样访问

          【讨论】:

          • 就是这样。元组成员的字段名称很好,使您的代码更容易理解。当然,我们应该有 Classes 和 Structs 来定义这样的配对,但是对于一次性的零碎(和快速原型制作),这可以很好地完成工作。注意:我了解到,如果您添加软件包然后升级到 4.7,您可能会遇到问题,直到您卸载它。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-25
          相关资源
          最近更新 更多