- Hands-On Object:Oriented Programming with C#
- Raihan Taher
- 196字
- 2021-07-02 12:44:44
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 );
}
推薦閱讀
- arc42 by Example
- Developing Middleware in Java EE 8
- MATLAB 2020 從入門到精通
- Elasticsearch for Hadoop
- Highcharts Cookbook
- Gradle for Android
- Apache Kafka Quick Start Guide
- Java面向對象程序設計
- 網絡數據采集技術:Java網絡爬蟲實戰
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- 零基礎PHP從入門到精通
- Python網絡運維自動化
- Selenium WebDriver自動化測試完全指南
- Java EE企業級應用開發教程:Spring+Spring MVC+MyBatis(第2版)
- Go Programming Blueprints