How to create a tuple with string and int items in C#?


Firstly, set two items in the tuple.

Tuple<int, string> tuple = new Tuple<int, string>(20, "Tom");

Now check for first item in the tuple, which is an integer.

if (tuple.Item1 == 20) {
   Console.WriteLine(tuple.Item1);
}

Now check for second item in the tuple, which is a string −

if (tuple.Item2 == "Tom") {
   Console.WriteLine(tuple.Item2);
}

The following is an example to create a tuple with string and int items.

Example

 Live Demo

using System;
using System.Threading;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         Tuple<int, string> tuple = new Tuple<int, string>(20, "Tom");
         if (tuple.Item1 == 20) {
            Console.WriteLine(tuple.Item1);
         }
         if (tuple.Item2 == "Tom") {
            Console.WriteLine(tuple.Item2);
         }
      }
   }
}

Output

20
Tom

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements