[You must know. NET] The 32nd time, deep. NET 4.0, tuple one or two

Source: Internet
Author: User
Tags int size

Tuple, is one of the concepts of functional programming, early in Elang, F # and other dynamic languages. However, the first time I heard that tuple in the 2005 garden of Ninputer Daniel raised the basic idea of implementing tuple in. NET 2.0, we can admire the historical fragments at that time by following the address:

Approach to the implementation of tuple in. NET 2.0

Thus, tuple is not the invention of. NET 4.0, but it is a necessary complement to the concept of C # tending to functional programming. So, first of all, let's take a look at what is tuple?

What is tuple?

What is tuple, in Chinese we translate it into tuples. The concept of tuple is derived from mathematical concepts that represent an ordered set of data. Tuple is implemented as a generic type in. NET, N-tuple represents the tuple of n elements, and the elements of a collection can be of any type, such as defining a 3-tuple to represent date (year, Month, day) when defined as:

// Release : code01, 2009/05/29
// Author  : Anytao, http://www.anytao.com
var date = Tuple.Create<int, int, int>(2009, 5, 29);

Through Tuple.create<int, int, int> will define a tuple<int, int, int> instance, which implements three data members:

For the specific analysis of tuple we then analyze, now only understand a general.

We can have two aspects of understanding, in. NET about tuple we have the following definition:

In a broad sense, tuple is a data structure, in general, the type and data of its members are determined.

In a narrow sense, all the types that implement the Ituple interface are examples of tuple. In. NET 4.0 BCL, 8 tuple types are predefined. For example, the simplest tuple is defined as:

public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
}

All other tuple types implement the Ituple interface, which is defined as:

interface ITuple
{
    int Size { get; }

    int GetHashCode(IEqualityComparer comparer);
    string ToString(StringBuilder sb);
}

In this interface, a read-only property of size, two overwrite methods GetHashCode and ToString is defined, and the tuple of the interface is as follows:

public class Tuple<T1>
public class Tuple<T1, T2>
public class Tuple<T1, T2, T3>
public class Tuple<T1, T2, T3, T4>
public class Tuple<T1, T2, T3, T4, T5>
public class Tuple<T1, T2, T3, T4, T5, T6>
public class Tuple<T1, T2, T3, T4, T5, T6, T7>
public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>

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.