c# - 列出超过 8 个项目的元组

标签 c# .net tuples

任何人都可以帮助以下 List Tuple more than 8 elements is not working:

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

出现如下错误

Error 1 The best overloaded method match for 'System.Collections.Generic.List<System.Tuple<int,string,double,string,int,string,double,System.Tuple<int,string>>>.Add(System.Tuple<int,string,double,string,int,string,double,System.Tuple<int,string>>)' has some invalid arguments C:\Users\Hewlett Packard\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 68 17 WindowsFormsApplication1 and Error 2 Argument 1: cannot convert from 'System.Tuple<int,string,double,string,int,string,double,System.Tuple<System.Tuple<int,string>>>' to 'System.Tuple<int,string,double,string,int,string,double,System.Tuple<int,string>>' C:\Users\Hewlett Packard\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 68 25 WindowsFormsApplication1

最佳答案

问题出在 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
)

基本上,包装 T8Tuple<T8>自动 - 并且有点无益。

您可以使用 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<T1 ... T7> 上的扩展方法:

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

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

关于c# - 列出超过 8 个项目的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24263598/

相关文章:

python - 如何忽略元组的解压缩部分作为 lambda 的参数?

python - 检查列表理解中可变数量的条件

C# 使用某种子方法创建方法

c# - 在 Fluent Nhibernate 中使用多个数据库,System.TypeInitializationException 未处理

c# - 解析 Json facebook c#

c# - 在单元测试中生成线程

python - 将嵌套元组转换为嵌套列表

c# - 如何识别USB插槽中插入的是什么设备?

c# - 跨 Asp.NET Core 和框架的数据保护提供程序(生成密码重置链接)

c# - 将 WCF 服务限制为特定用户帐户的最佳方法