How to create 1-Tuple or Singleton Tuple in C#?

The Tuple<T1> class in C# represents a 1-tuple or singleton tuple, which is a data structure that holds a single element. Unlike regular variables, tuples provide additional functionality and can be useful when you need to treat a single value as part of a tuple-based system.

Syntax

Following is the syntax for creating a 1-tuple −

Tuple<T> tupleName = new Tuple<T>(value);

Accessing the value using the Item1 property −

T value = tupleName.Item1;

Using 1-Tuple with Integer Values

The following example demonstrates creating and using a 1-tuple with an integer value −

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("Found: " + tuple.Item1);
      }
      
      if (tuple.Item1 == 100) {
         Console.WriteLine("Match found: " + tuple.Item1);
      }
   }
}

The output of the above code is −

Value = 100
Match found: 100

Using 1-Tuple with String Values

The following example shows how to work with string values in a 1-tuple −

using System;

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

The output of the above code is −

Value = amit
Length = 4
Exists: Tuple Value = amit

Alternative Creation Methods

You can also create 1-tuples using the static Tuple.Create() method −

using System;

public class Demo {
   public static void Main(string[] args) {
      var numberTuple = Tuple.Create(42);
      var textTuple = Tuple.Create("Hello World");
      
      Console.WriteLine("Number tuple: " + numberTuple.Item1);
      Console.WriteLine("Text tuple: " + textTuple.Item1);
      Console.WriteLine("Number tuple type: " + numberTuple.GetType());
   }
}

The output of the above code is −

Number tuple: 42
Text tuple: Hello World
Number tuple type: System.Tuple`1[System.Int32]

Common Use Cases

  • Method Return Values: When you need to return a single value but want tuple-like behavior for consistency with other methods.

  • Generic Programming: When working with generic collections or methods that expect tuple types.

  • Data Processing: When processing data structures where single-element tuples maintain uniformity with multi-element tuples.

Conclusion

The 1-tuple or singleton tuple in C# provides a way to wrap a single value in tuple format using Tuple<T>. While less common than multi-element tuples, singleton tuples can be useful in generic programming scenarios and when maintaining consistency in tuple-based data processing systems.

Updated on: 2026-03-17T07:04:35+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements