How to create 1-Tuple or Singleton Tuple 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.

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: 06-Nov-2019

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements