Meta-group
Arrays combine objects of the same type , and tuples combine different types of objects .
. NET 4 defines 8 generic tuple classes and a static tuple class . They are used as a factory for tuples .
A tuple is a data structure that is separated by commas
The tuple<t1,t2,t3...> can be up to 7, or a tuple of eight or more elements can be created through the objects in the nested tuple in the Rest Property Tuple<t1, T2, T3, T4, T5, T6, T7, trest> object.
Case :
tuple of an element
tuple<int> test = new Tuple<int> (34);
two elements of a tuple
tuple<string, int> test2 = new tuple<string, int> ("Zhangsan", 23);
tuple of multiple elements
Tuple<int, int, tuple<int, int>> test_i_i_tii = new Tuple<int, int, tuple<int, int>> (1, 1, new Tup Le<int, Int> (2, 3));
Console.WriteLine (Test. ITEM1);
Console.WriteLine ("{0}: {1}", Test2. Item1,test2. ITEM2);
Console.readkey ();
Multiple return value issues
General shiyongout keywords , using tuples can also be implemented , case :
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
namespace meta-group
{
Class Program
{
static void Main (string[] args)
{
String outparam = "";
using the Out keyword to get multiple return values
int returnvalue = Outtest (out outparam);
Console.WriteLine (Outparam);
use Tuple to get multiple keywords
tuple<string, int> test = Tupletest ();
Console.WriteLine ("{0},{1}", Test. ITEM1, Test. ITEM2);;
Console.readkey ();
}
<summary>
implementation returns two values
</summary>
<param name= "Test" ></param>
<returns></returns>
public static int Outtest (out string test)
{
test= "hahaha";
return 10;
}
<summary>
returns two values using a tuple implementation
</summary>
<returns></returns>
public static tuple<string, int> tupletest ()
{
return new tuple<string, int> ("Zhangsan", 10);
}
}
}
Of course, using the same tuple object created by the helper method is simpler :
var test = tuple.create ("Zhangsan", 19); Console.WriteLine ("{0},{1}", Test. Item1,test. ITEM2);
Tuples are typically used in four different ways :
1. Represents a set of data . For example , tuples can represent a database record , and its formation can be a moisturizing record of the various fields .
2. Provides easy access and operation of the data set
3. Without using out parameters ( in C # ) or ByRef parameters in The case of VB , Multiple values are returned from the method rollup
4. Pass multiple values to a method with a single parameter . Case :
static void Main (string[] args)
{
tuple<string,int,char> t=new tuple<string,int,char> ("Zhangsan", +, ' M ')
int res = Test (t);
}
public static int Test (Tuple<string,int,char> t)
{
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # Advanced Programming 36-day----tuples