About the tuple class added in. Net 4.0

Source: Internet
Author: User

Reference:

In many cases, we need a method to return multiple values. There are multiple ways to achieve this.

1. Use global variables

2. Use the out and ref Parameters

3. encapsulate multiple return values as struct or Class

4. Use tuple

 

. Net 4.0 adds an interesting tuple class to the base class library, which represents an ordered n tuples. The so-called tuples are actually "value pairs". For example, the following is a triple: (100,200,300)

You can call the tuple. create static method or use the new keyword to directly create a tuple object. The. NET class library defines the generic tuple with 1-7 Generic parameters.

Using the tuple object as the return value of the method can easily contain multiple results.

 Namespace Divideusetuple
{
Class Program
{
Static Void Main ( String [] ARGs)
{

Tuple < Int , Int > Result = divide ( 10 , 3 );
Console. writeline ( " {0} {1} " , Result. Item1, result. item2 ); // Output "3 1"
}
Static Tuple < Int , Int > Divide ( Int X, Int Y)
{
Return New Tuple < Int , Int > (X/y, X % Y );
}

}
}

Tuple implements the istructuralequatable, istructuralcomparable, and icomparable interfaces, which means that tuple variables can be directly compared.

 

The above is just used as a reference to introduce tuple. In fact, tuple can be used for more purposes.

To be used later ....

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.