English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# Object-Oriented (OOP)4The Tuple<T> class is introduced in .NET Framework
.0 introduced. Tuple is a data structure that contains a sequence of elements of different data types. It can be used when you need a data structure to save objects with properties, but do not want to create a separate type for them.
Syntax:1Tuple<T2Tuple<T3Tuple<T4Tuple<T5Tuple<T6Tuple<T7, T
, TRest>
Tuple<int, string, string> person = new Tuple<int, string, string>(1, "Steve", "Jobs");
In the above example, we created a Tuple containing an instance of a person's record. We specify a type for each element and pass the value to the constructor. It is麻烦 to specify the type for each element. C# introduces a static helper class Tuple, which returns an instance of Tuple <T> without specifying the type of each element, as shown below.
var person = Tuple.Create(1, "Steve", "Jobs");
A tuple can contain a maximum of eight elements. When you try to include more than eight elements, it will produce a compiler error.
var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
tuple elements can be accessed through the Item < elementnumber > property, for example Item1、 Item2、 Item3etc., you can access Item7property. Item1property returns the first element, Item2Return the second element, and so on. The last element (the8an element) will use the Rest property to return.
var person = Tuple.Create(1, "Steve", "Jobs"); person.Item1; // Return 1 person.Item2; // return "Steve" person.Item3; // return "Jobs" var numbers = Tuple.Create("One", 2, 3, "Four", 5, "Six", 7, 8); numbers.Item1; // Returns "One" numbers.Item2; // Return 2 numbers.Item3; // Return 3 numbers.Item4; // Returns "Four" numbers.Item5; // Return 5 numbers.Item6; // Returns "Six" numbers.Item7; // Return 7 numbers.Rest; // Return (8) numbers.Rest.Item1; // Return 8
Typically, the8positions for nested tuples, you can use the Rest property to access that position.
If you want to include eight or more elements in a tuple, you can do so by nesting another tuple object as the eighth element. You can access the last nested tuple using the Rest property. To access elements of a nested tuple, use Rest.Item1.Item<elelementNumber> property.
var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, 9, 10, 11, 12, 13)); numbers.Item1; // Return 1 numbers.Item7; // Return 7 numbers.Rest.Item1; //Return (8,9,10,11,12,13) numbers.Rest.Item1.Item1; //Return 8 numbers.Rest.Item1.Item2; //Return 9
You can include nested tuple objects at any position in the sequence. However, it is recommended to place nested tuples at the end of the sequence so that the Rest property can be used to access it.
var numbers = Tuple.Create(1, 2, Tuple.Create(3, 4, 5, 6, 7, 8), 9, 10, 11, 12, 13 ); numbers.Item1; // Return 1 numbers.Item2; // Return 2 numbers.Item3; // Return (3, 4, 5, 6, 7, 8) numbers.Item3.Item1; // Return 3 numbers.Item4; // Return 9 numbers.Rest.Item1; //Return13
Methods can take tuples as parameters.
static void Main(string[] args) { var person = Tuple.Create(1, "Steve", "Jobs"); DisplayTuple(person); } static void DisplayTuple(Tuple<int,string,string> person) { Console.WriteLine($"Id = { person.Item1}); Console.WriteLine($"First Name = { person.Item2}); Console.WriteLine($"Last Name = { person.Item3}); }
Tuples can be returned from a method.
static void Main(string[] args) { var person = GetPerson(); } static Tuple<int, string, string> GetPerson() { return Tuple.Create(1, "Bill", "Gates"); }
Tuples can be used in the following situations:
When you want to return multiple values from a method without using ref or out parameters...
When you want to pass multiple values to a method through a single parameter...
When you want to temporarily save database records or certain values without creating a separate class...
Tuple is a reference type, not a value type. It is allocated on the heap and may cause CPU-intensive operations.
Tuple is limited to including eight elements. If you need to store more elements, you need to use nested tuples. However, this may lead to ambiguity.
It is not reasonable to access Tuple elements using the naming pattern Item <elementNumber> of the property.
C#7ValueTuple was introduced to overcome the limitations of Tuple and make the use of Tuple easier. Learn about it in the next chapter.