Tuple Class in C#


The Tuple<T1> class represents a 1-tuple, which is called singleton. A tuple is a data structure that has a sequence of elements.

It is used in −

  • Easier access to a data set.
  • Easier manipulation of a data set.
  • To represent a single set of data.
  • To return multiple values from a method
  • To pass multiple values to a method

It has one property −

  • Item1 − Get the value of the current Tuple<T1> object's first component.

Example

Let us now see an example to implement the 1-tuple in C# −

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<int> tuple = new Tuple<int>(100);
      Console.WriteLine("Value = " + tuple.Item1);
      if (tuple.Item1 == 150) {
         Console.WriteLine(tuple.Item1);
      }
      if (tuple.Item1 == 100) {
         Console.WriteLine(tuple.Item1);
      }
   }
}

Output

This will produce the following output −

Value = 100
100

Example

Let us now see another example to implement the 1-tuple in C# −

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<string> tuple = new Tuple<string>("amit");
      Console.WriteLine("Value = " + tuple.Item1);
      if (tuple.Item1 == "tom") {
         Console.WriteLine(tuple.Item1);
      }
      if (tuple.Item1 == "amit") {
         Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
      }
   }
}

Output

This will produce the following output −

Value = amit
Exists: Tuple Value = amit

Updated on: 05-Nov-2019

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements