程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#元組 Pair Triplet Tuple

C#元組 Pair Triplet Tuple

編輯:C#入門知識

C#默認提供了Pair、Triplet 、Tuple(4.0新增加的)這三個元組,在一些小的數據結構使用時,我們可以不用去聲明相應的數據結構,而用這些元組來替代。

[csharp]
public sealed class Pair 

    // Fields  
    public object First; 
    public object Second; 
 
    // Methods  
    public Pair() 
    { 
    } 
 
    public Pair(object x, object y) 
    { 
        this.First = x; 
        this.Second = y; 
    } 

public sealed class Pair
{
    // Fields
    public object First;
    public object Second;

    // Methods
    public Pair()
    {
    }

    public Pair(object x, object y)
    {
        this.First = x;
        this.Second = y;
    }
}
[csharp]
[Serializable] 
public sealed class Triplet 

    // Fields  
    public object First; 
    public object Second; 
    public object Third; 
 
    // Methods  
    public Triplet() 
    { 
    } 
 
    public Triplet(object x, object y) 
    { 
        this.First = x; 
        this.Second = y; 
    } 
 
    public Triplet(object x, object y, object z) 
    { 
        this.First = x; 
        this.Second = y; 
        this.Third = z; 
    } 

[Serializable]
public sealed class Triplet
{
    // Fields
    public object First;
    public object Second;
    public object Third;

    // Methods
    public Triplet()
    {
    }

    public Triplet(object x, object y)
    {
        this.First = x;
        this.Second = y;
    }

    public Triplet(object x, object y, object z)
    {
        this.First = x;
        this.Second = y;
        this.Third = z;
    }
}
以上這2個東東是表示2個、3個屬性的可序列化對象。但是大家一定要注意它們的屬性都是object,命名空間是System.Web.UI也就是說它們在System.Web.dll。

在。net4.0中 C#新增了Tuple 可以表示1-8個強類型屬性的對象,在mscorlib.dll的System命名空間下

[csharp]
public static class Tuple 

    // Methods  
    internal static int CombineHashCodes(int h1, int h2) 
    { 
        return (((h1 << 5) + h1) ^ h2); 
    } 
 
    internal static int CombineHashCodes(int h1, int h2, int h3) 
    { 
        return CombineHashCodes(CombineHashCodes(h1, h2), h3); 
    } 
 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4) 
    { 
        return CombineHashCodes(CombineHashCodes(h1, h2), CombineHashCodes(h3, h4)); 
    } 
 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5) 
    { 
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), h5); 
    } 
 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6) 
    { 
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6)); 
    } 
 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7) 
    { 
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7)); 
    } 
 
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8) 
    { 
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7, h8)); 
    } 
 
    public static Tuple<T1> Create<T1>(T1 item1) 
    { 
        return new Tuple<T1>(item1); 
    } 
 
    public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2) 
    { 
        return new Tuple<T1, T2>(item1, item2); 
    } 
 
    public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3) 
    { 
        return new Tuple<T1, T2, T3>(item1, item2, item3); 
    } 
 
    public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4) 
    { 
        return new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4); 
    } 
 
    public static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5) 
    { 
        return new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5); 
    } 
 
    public static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6) 
    { 
        return new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6); 
    } 
 
    public static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7) 
    { 
        return new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7); 
    } 
 
    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) 
    { 
        return new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>(item1, item2, item3, item4, item5, item6, item7, new Tuple<T8>(item8)); 
    } 

public static class Tuple
{
    // Methods
    internal static int CombineHashCodes(int h1, int h2)
    {
        return (((h1 << 5) + h1) ^ h2);
    }

    internal static int CombineHashCodes(int h1, int h2, int h3)
    {
        return CombineHashCodes(CombineHashCodes(h1, h2), h3);
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4)
    {
        return CombineHashCodes(CombineHashCodes(h1, h2), CombineHashCodes(h3, h4));
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5)
    {
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), h5);
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6)
    {
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6));
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7)
    {
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7));
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8)
    {
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7, h8));
    }

    public static Tuple<T1> Create<T1>(T1 item1)
    {
        return new Tuple<T1>(item1);
    }

    public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
    {
        return new Tuple<T1, T2>(item1, item2);
    }

    public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
    {
        return new Tuple<T1, T2, T3>(item1, item2, item3);
    }

    public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4)
    {
        return new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4);
    }

    public static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
    {
        return new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
    }

    public static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
    }

    public static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
    }

    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)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>(item1, item2, item3, item4, item5, item6, item7, new Tuple<T8>(item8));
    }
}
其中的Tuple<...>都有[Serializable]屬性表示可序列化。可見Tuple只是一個簡單的工廠,使用它可以創建Tuple<...>類型的對象了。

 作者:dz45693
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved