Home  >  Article  >  Backend Development  >  What are tuples in C# 4.0?

What are tuples in C# 4.0?

王林
王林forward
2023-08-30 23:25:06624browse

C#4.0 中的元组是什么?

Tuples have sequences of elements of different data types. It was introduced to return an instance of Tuple8742468051c85b06f0a0af9e3e506b5c without having to specify the type of each element individually.

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 -

Example

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);
         }
      }
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete