What are tuples in C# 4.0?

王林
Release: 2023-08-30 23:25:06
forward
655 people have browsed it

C#4.0 中的元组是什么?

Tuples have sequences of elements of different data types. It was introduced to return an instance of Tuple 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");
Copy after login

Now, for example, check the first item in the tuple, it is an integer -

if (tuple.Item1 == 99) {
   Console.WriteLine(tuple.Item1);
}
Copy after login

Now check the second item in the tuple, it is a string -

if (tuple.Item2 == "Steve") {
   Console.WriteLine(tuple.Item2);
}
Copy after login

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);
         }
      }
   }
}
Copy after login

Output

50
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template