標籤:des style blog io ar color os sp for
Tuple 字面意思:元組。是.net4.0增加的新特性,是幹什麼的呢?總結一句,個人覺得這個東西 就是用來在有返回很多種類型的值時可以用到。它提供了8種類型的Tuple,直接看最複雜的那種(其實不是複雜,就是參數比別人多而已,原理是完全一致的),原理不講太多,看下這個類的結構就知道什麼原理了。
[Serializable, __DynamicallyInvokable]public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple{ // Fields private readonly T1 m_Item1; private readonly T2 m_Item2; private readonly T3 m_Item3; private readonly T4 m_Item4; private readonly T5 m_Item5; private readonly T6 m_Item6; private readonly T7 m_Item7; private readonly TRest m_Rest; // Methodspublic Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest); public override bool Equals(object obj); public override int GetHashCode(); int IStructuralComparable.CompareTo(object other, IComparer comparer); bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer); int IStructuralEquatable.GetHashCode(IEqualityComparer comparer); int IComparable.CompareTo(object obj); int ITuple.GetHashCode(IEqualityComparer comparer); string ITuple.ToString(StringBuilder sb); public override string ToString(); // Properties [__DynamicallyInvokable] public T1 Item1 { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; } 此處省略:T2到T6的定義,因為完全一致,為了減少文章篇幅。 [__DynamicallyInvokable] public T7 Item7 { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; } [__DynamicallyInvokable] public TRest Rest { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; }//注意這個最後一個參數的類型,其實它是Tupple類型的 int ITuple.Size { get; }}
Tuple<int, string, bool, char, ClassStyle, Dictionary<int,string>, IEnumerable<int> , TRest> tuple=new
Tuple(1,"string",true,‘1‘,new Student(),.....,Tuple<任意類型>);
用法就是在函數返回的時候,可以new一個這樣的類型的對象,對其各個屬性賦值,這樣就可以返回非常多類型的資料,取得返回的值,就是用這個tuple對象.Item1,就可以獲得相應類型的值,比如在這裡我們tuple.Item1的值是1,tuple.Item2的值是“string”,以此類推。當然這些Item是唯讀,不能對其進行賦值操作。為我們提供了很大的方便。以上是建立Tuple的方法之一,接下來說下另一個方法,先看下這個Tuple靜態類:
[__DynamicallyInvokable]public static class Tuple{ // Methods internal static int CombineHashCodes(int h1, int h2);internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8); [__DynamicallyInvokable] public static Tuple<T1> Create<T1>(T1 item1);
此處省略:T2到T6的定義,因為完全一致,為了減少文章篇幅。
[__DynamicallyInvokable]
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); }
其實代碼更多,當然區別也是參數個數不一致,原理還是一樣的,這裡唯寫出兩個。
看一眼就知道Create<....>這個方法,沒錯我們也可以通過Tuple這個靜態類中的Create靜態方法來建立不定個數參數的Tuple對象,舉個例子:
var trest = Tuple.Create<int>(8);var t8 = Tuple.Create<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, trest);
第八個參數 一定是要Tuple類型的,當然只有這個需要8個參數的Tuple類型最後一個參數需要Tuple類型,而其他幾個(這裡指1~7個參數的Tuppe)都是任意類型都可以。可能是為了用於擴充更多的參數,因為Tuple畢竟只是提供了最多8種不同參數的構造方法。其實這個東西就是為了方便我們可以返回多個不同類型的值。
.Net 4.0特性 Tuple元組