C # tuples Pair Triplet Tuple

Source: Internet
Author: User

C # The Pair, Triplet, and Tuple (New in 4.0) tuples are provided by default. When using some small data structures, we do not need to declare the corresponding data structures, replace them with these tuples.

[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;
}
}
The above two items are serializable objects that represent two or three attributes. However, you must note that all their attributes are objects, and the namespace is System. Web. UI, that is, they are in System. Web. dll.

In. In net4.0, C # adds a Tuple that can represent 1-8 strongly-typed attributes, which is in the System namespace of mscorlib. dll.

[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 (h1, h2), h3 );
}
 
Internal static int CombineHashCodes (int h1, int h2, int h3, int h4)
{
Return CombineHashCodes (h1, h2), CombineHashCodes (h3, h4 ));
}
 
Internal static int CombineHashCodes (int h1, int h2, int h3, int h4, int h5)
{
Return CombineHashCodes (h1, h2, h3, h4), h5 );
}
 
Internal static int CombineHashCodes (int h1, int h2, int h3, int h4, int h5, int h6)
{
Return 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 (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 (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 (h1, h2), h3 );
}

Internal static int CombineHashCodes (int h1, int h2, int h3, int h4)
{
Return CombineHashCodes (h1, h2), CombineHashCodes (h3, h4 ));
}

Internal static int CombineHashCodes (int h1, int h2, int h3, int h4, int h5)
{
Return CombineHashCodes (h1, h2, h3, h4), h5 );
}

Internal static int CombineHashCodes (int h1, int h2, int h3, int h4, int h5, int h6)
{
Return 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 (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 (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 ));
}
}
The Tuple <...> all have the [Serializable] attribute to indicate that it can be serialized. It can be seen that Tuple is just a simple factory. You can use it to create Tuple <...> type objects.

Author: dz45693
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.