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 ....