簡體   English   中英

列出超過 8 項的元組

[英]List Tuple more than 8 items

任何人都可以幫助以下列表元組超過 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<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>>) ' 有一些無效參數 C:\\Users\\Hewlett Packard\\AppData\\Local\\Temporary Projects\\WindowsFormsApplication1\\Form1。 cs 68 17 WindowsFormsApplication1 和錯誤 2 參數 1:無法從“ System.Tuple<int,string,double,string,int,string,double,System.Tuple<System.Tuple<int,string>>> ”轉換為“ 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
)

基本上,它會自動 T8 包裝Tuple<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"));

我個人試圖完全避免使用這個大小的元組 - 創建一個具有適當屬性的命名類型。

我真的不明白為什么我自己,但是當你使用new Tuple<>而不是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")
  )
);

在C#7中

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

我知道這是舊的,但我掙扎了好幾個小時,我終於用下面的方法讓它工作了:方法:

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 個,即使 item 8 是嵌套元組,然后經過許多小時的谷歌和挫折我發現這個出來。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM