【问题标题】:List Tuple more than 8 items列出超过 8 个项目的元组
【发布时间】:2014-08-07 10:52:38
【问题描述】:

任何人都可以帮助以下列表元组超过 8 个元素不起作用:

List<Tuple<int, string, double, string, int, string, double, Tuple<int, string>>> tpl = new 
List<Tuple<int, string, double, string, int, string, double, Tuple<int, string>>>();
tpl.Add(Tuple.Create(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123, new Tuple<int, string>(100, "My Rest Item")));

foreach(var k in tpl)
        listBox1.Items.Add(k.Item1.ToString() + " ---> " + k.Item2.ToString() + " ---> " + k.Item3.ToString() + " ---> " +
        k.Item4.ToString() + " ---> " + k.Item5.ToString() + " ---> " + k.Item6.ToString() + " ---> " +
        k.Item7.ToString() + " ---> " + k.Rest.Item1.ToString());

它给出以下错误

错误 1 ​​最好的重载方法匹配 'System.Collections.Generic.List&lt;System.Tuple&lt;int,string,double,string,int,string,double,System.Tuple&lt;int,string&gt;&gt;&gt;.Add(System.Tuple&lt;int,string,double,string,int,string,double,System.Tuple&lt;int,string&gt;&gt;)' 有一些无效参数 C:\Users\Hewlett Packard\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 68 17 WindowsFormsApplication1 和错误 2 参数 1:无法从 'System.Tuple&lt;int,string,double,string,int,string,double,System.Tuple&lt;System.Tuple&lt;int,string&gt;&gt;&gt;' 到 'System.Tuple&lt;int,string,double,string,int,string,double,System.Tuple&lt;int,string&gt;&gt;' C:\Users\Hewlett Packard\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 68 25 WindowsFormsApplication1

【问题讨论】:

  • 除此之外 - 不要这样做!使用相关属性等创建您自己的命名类型。

标签: c# .net tuples


【解决方案1】:

我自己也不太明白为什么,但是当您使用 new Tuple&lt;&gt; 而不是 Tuple.Create 时,代码会起作用:

tpl.Add
( new Tuple<int, string, double, string, int, string, double, Tuple<int, string>>
  ( 1
  , "ABC"
  , 100.123
  , "XYZ"
  , 1
  , "ABC"
  , 100.123
  , Tuple.Create(100, "My Rest Item")
  )
);

【讨论】:

    【解决方案2】:

    问题在于Tuple.Create 的最后一个参数。仔细看参数返回值是如何定义的:

    public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>
        Create<T1, T2, T3, T4, T5, T6, T7, T8>(
        T1 item1,
        T2 item2,
        T3 item3,
        T4 item4,
        T5 item5,
        T6 item6,
        T7 item7,
        T8 item8
    )
    

    基本上, T8 自动包装在 Tuple&lt;T8&gt; 中 - 有点无益。

    您可以改用new

    var value = new Tuple<<int, string, double, string, int, string, double,
                          Tuple<int, string>>
        (1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123,
         new Tuple<int, string>(100, "My Rest Item"));
    

    虽然这很可怕。自己创建一些静态方法可能会更好,例如

    public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8, T9>>
        Create(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8, T9>>
            (t1, t2, t3, t4, t5, t6, t7, Tuple.Create(t8, t9)); 
    }
    

    (根据需要使用尽可能多的重载)

    或者可能是Tuple&lt;T1 ... T7&gt;上的扩展方法:

    public static Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
        With(this Tuple<T1, T2, T3, T4, T5, T6, T7> tuple,
             TRest rest)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(
            tuple.Item1, 
            tuple.Item2, 
            tuple.Item3, 
            tuple.Item4, 
            tuple.Item5, 
            tuple.Item6, 
            tuple.Item7,
            rest);
    }
    

    那么你可以使用:

    var value = Tuple.Create(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123)
                     .With(Tuple.Create(100, "My Rest Item"));
    

    我个人会尽量避免完全使用这种大小的元组 - 而是创建一个具有适当属性的命名类型。

    【讨论】:

    • 那么这将是该工厂方法声明中的错误吗?我真的看不出这样做有什么意义。
    • @LasseV.Karlsen:我怀疑有一些原因,但并不完全清楚它是什么——在这个用例中肯定没有帮助。
    【解决方案3】:

    在 C#7 中

    var tup = (1, 2, 3, 4, 5, 6, 7, 8, "nine");
    var one = tup.Item1;
    var nine = tup.Item9;
    

    【讨论】:

    • 非常相关的提及它
    • 请注意,这会创建一个 ValueTuple 而不是 Tuple
    【解决方案4】:

    我知道这已经过时了,但我挣扎了好几个小时,终于用下面的方法让它工作了: 方法:

    internal static Tuple<List<double>, List<string>, List<string>, List<string>, List<string>, List<int>, List<int>, Tuple<List<string>, List<int>>> YourMethodName(int something)
    
    // do method work
    
    return new Tuple<List<double>, List<string>, List<string>, List<string>, List<string>, List<int>, List<int>, Tuple<List<string>, List<int>>> (itme1, item2, item3, item4, item5, item6, item7, new Tuple<List<string>, List<int>>(item8, item9));
    

    我正在使用 return Tuple.Create(item1, item2 etc) 并且它一直在工作,直到我尝试使用 item9 (它最多只能使用 8 个,即使第 8 项是嵌套元组,然后经过几个小时的谷歌和沮丧我发现了这一点。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-29
      • 2015-07-24
      • 2016-12-23
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多