Tuples have sequences of elements of different data types. It was introduced to return an instance of Tuple
Let's create a tuple with two elements. Here's how to declare a tuple. −
Tuple<int, string>person = new Tuple <int, string>(32, "Steve");
Now, for example, check the first item in the tuple, it is an integer -
if (tuple.Item1 == 99) { Console.WriteLine(tuple.Item1); }
Now check the second item in the tuple, it is a string -
if (tuple.Item2 == "Steve") { Console.WriteLine(tuple.Item2); }
The following is an example of creating a tuple containing string and integer items -
Live demonstration
using System; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { Tuple<int, string> tuple = new Tuple<int, string>(50, "Tom"); if (tuple.Item1 == 50) { Console.WriteLine(tuple.Item1); } if (tuple.Item2 == "Jack") { Console.WriteLine(tuple.Item2); } } } }
50
The above is the detailed content of What are tuples in C# 4.0?. For more information, please follow other related articles on the PHP Chinese website!