官术网_书友最值得收藏!

Tuples

A tuple is a data structure that holds a set of data. Tuples are mainly helpful when you want to group data and use it. Normally, a C# method can only return one value. By using a tuple, it is possible to return multiple values from a method. The Tuple class is available under the System.Tuple namespace. A tuple can be created using the Tuple<> constructor or by an abstract method named Create that comes with the Tuple class.

You can fix any data type in a tuple and access it using Item1, Item2, and so on. Let's look at an example to get a better idea of this:

var person = new Tuple<string, int, string>("Martin Dew", 42, "Software Developer"); // name, age, occupation
or
var person = new Tuple.Create("Martin Dew", 42, "Software Developer");

Let's take a look at how to return a tuple from a method by using the following code:

public static Tuple<string, int, string> GetPerson() {
var person = new Tuple<string, int, string>("Martin Dew", 42, "Software Developer");
return person;
}
static void Main() {
var developer = GetPerson();
Console.WriteLine("The person is {0}. He is {1} years old. He is a {2}", developer.Item1, developer.Item2, developer.Item3 );
}
主站蜘蛛池模板: 博湖县| 闽侯县| 肃南| 清流县| 称多县| 若尔盖县| 修文县| 怀远县| 金门县| 乐东| 遂溪县| 平泉县| 台中县| 西吉县| 澄江县| 博湖县| 临城县| 巴南区| 连平县| 花莲县| 北京市| 赤城县| 英吉沙县| 依安县| 镇江市| 苏尼特右旗| 昌乐县| 自贡市| 红河县| 鄂托克前旗| 无锡市| 曲沃县| 澎湖县| 黑水县| 东乌珠穆沁旗| 南漳县| 济南市| 石家庄市| 永丰县| 宁远县| 鲁山县|