2014-06-17 23 views
3

誰能幫助下面的列表元組超過8個元素是不工作:列表元組超過800項

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 and Er ROR 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:\用戶\惠普 惠普\應用程序數據\本地\臨時 項目\ WindowsFormsApplication1 \ Form1.cs中68 25 WindowsFormsApplication1

+14

除了 - 不要這樣做!用相關屬性等創建你自己的命名類型。 –

回答

3

我真的不明白,爲什麼我自己,但是當你用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") 
) 
); 
11

的問題是與最後一個參數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")); 

我個人」儘量避免使用這個元組的大小 - 創建一個帶有這個約束的命名類型代之以priate屬性。

+1

那麼這將是該工廠方法聲明中的一個錯誤呢?我真的不明白這一點。 –

+4

@ LasseV.Karlsen:我懷疑它有一些原因,但它不完全清楚它是什麼 - 在這個用例中它肯定是無益的。 –

1

在C#7

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