Examples of C # tuples are detailed

Source: Internet
Author: User

Summary of tuples:

Arrays combine objects of the same type, and tuples combine different types of objects. Tuples originate from functional programming languages (such as F #),
Tuples are frequently used in these languages. In N-Stare 4, tuples can be used with. NET fmmework for all NET languages.
. NET 4 defines 8 generic tuple classes and a static tuple class that are used as a factory for tuples. Here are the different generic tuple
class supports a different number of elements. For example,tuple<t1> contains an element,tuple<t1,t2> contains two elements, and so on.

1. A first example

Private Tuple<int, int> Divide (int dividend, int divisor)
{
int result = Dividend/divisor;
int reminder = dividend% divisor;
Return tuple.create<int, int> (result, reminder); Returns a tuple of two elements of the same type
}

--------Test-------------

private void Button1_Click (object sender, EventArgs e)
{
Tuple<int, int> result = Divide (13, 2);
Console.WriteLine ("Result of divison:{0}," +
"Reminder:{1}", result. ITEM1, result.                ITEM2); Accessing tuples ' items with property item1,item2
}

-------Results-------------

Result of Divison:6,reminder:1

2. A second example

Private Tuple<int, string> MyTest2 (int dividend, string Name)
{
int result = DIVIDEND/2;
String name = "Hello," + name;
Return tuple.create<int, string> (result, name); Returns a tuple of two different types of elements
}

--------Test-------------

private void Button2_Click (object sender, EventArgs e)
{
Tuple<int, string> result = MyTest2 ("Limin");
Console.WriteLine ("Result of divison:{0}," +
"Name:{1}", result. ITEM1, result.                ITEM2); Accessing tuples ' items with property item1,item2
}

-------Results-------------

Result of Divison:6,name:hello,limin

3. A third example

If a tuple contains more than 8 items, you can use a tuple class definition with 8 parameters. The last template parameter is trest,
Indicates that a tuple must be passed to it. This allows you to create a tuple with any parameters.
This feature is described below:
public class Tuple<t1,t2,t3,t4,t5,t6,t7,trest>
Where the last template parameter is a tuple type, you can create tuples with any number of items:
var tuple = tuple.create<string,string,string, int,int,int,double,
Tuple<int,int>> (
"Stephanie", "Alina", "Nagel", 2009,6,2,1.37,
Tuple.create<int,int> (52,3490));

Examples of C # tuples are detailed

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.