How to create 4-Tuple or quadruple in C#?

The Tuple<T1, T2, T3, T4> class represents a 4-tuple, which is called a quadruple. A tuple is a data structure that holds a sequence of elements of different types in a single object.

4-tuples are commonly used for −

  • 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.

4-Tuple Structure Item1 Item2 Item3 Item4 <T1> <T2> <T3> <T4> Each element can have a different data type

Syntax

Following is the syntax to create a 4-tuple −

Tuple<T1, T2, T3, T4> tuple = new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4);

You can also use the Tuple.Create() method −

var tuple = Tuple.Create(item1, item2, item3, item4);

Using String 4-Tuple

Let us see an example to implement a 4-tuple with string values −

using System;

public class Demo {
   public static void Main(string[] args) {
      Tuple<string, string, string, string> tuple = new Tuple<string, string, string, string>("nathan", "steve", "katie", "tim");
      Console.WriteLine("Value (Item1)= " + tuple.Item1);
      Console.WriteLine("Value (Item2)= " + tuple.Item2);
      Console.WriteLine("Value (Item3)= " + tuple.Item3);
      Console.WriteLine("Value (Item4)= " + tuple.Item4);
      
      if (tuple.Item1 == "nathan") {
         Console.WriteLine("Exists: Tuple Value = " + tuple.Item1);
      }
      if (tuple.Item2 == "jack") {
         Console.WriteLine("Exists: Tuple Value = " + tuple.Item2);
      }
      if (tuple.Item3 == "katie") {
         Console.WriteLine("Exists: Tuple Value = " + tuple.Item3);
      }
      if (tuple.Item4 == "tim") {
         Console.WriteLine("Exists: Tuple Value = " + tuple.Item4);
      }
   }
}

The output of the above code is −

Value (Item1)= nathan
Value (Item2)= steve
Value (Item3)= katie
Value (Item4)= tim
Exists: Tuple Value = nathan
Exists: Tuple Value = katie
Exists: Tuple Value = tim

Using Integer 4-Tuple

Let us see another example to implement a 4-tuple with integer values −

using System;

public class Demo {
   public static void Main(string[] args) {
      Tuple<int, int, int, int> tuple = new Tuple<int, int, int, int>(100, 150, 300, 450);
      Console.WriteLine("Value (Item1)= " + tuple.Item1);
      Console.WriteLine("Value (Item2)= " + tuple.Item2);
      Console.WriteLine("Value (Item3)= " + tuple.Item3);
      Console.WriteLine("Value (Item4)= " + tuple.Item4);
      
      if (tuple.Item1 == 100) {
         Console.WriteLine("Exists: Tuple Item 1 = " + tuple.Item1);
      }
      if (tuple.Item2 == 250) {
         Console.WriteLine("Exists: Tuple Item 2 = " + tuple.Item2);
      }
      if (tuple.Item3 == 270) {
         Console.WriteLine("Exists: Tuple Item 3 = " + tuple.Item3);
      }
      if (tuple.Item4 == 300) {
         Console.WriteLine("Exists: Tuple Item 4 = " + tuple.Item4);
      }
   }
}

The output of the above code is −

Value (Item1)= 100
Value (Item2)= 150
Value (Item3)= 300
Value (Item4)= 450
Exists: Tuple Item 1 = 100

Using Mixed Data Types

A 4-tuple can hold different data types in each position −

using System;

public class Demo {
   public static void Main(string[] args) {
      Tuple<string, int, double, bool> mixedTuple = Tuple.Create("John", 25, 75.5, true);
      
      Console.WriteLine("Name: " + mixedTuple.Item1);
      Console.WriteLine("Age: " + mixedTuple.Item2);
      Console.WriteLine("Weight: " + mixedTuple.Item3);
      Console.WriteLine("Is Active: " + mixedTuple.Item4);
      
      // Calculate BMI example
      double height = 1.75; // meters
      double bmi = mixedTuple.Item3 / (height * height);
      Console.WriteLine("BMI: " + Math.Round(bmi, 2));
   }
}

The output of the above code is −

Name: John
Age: 25
Weight: 75.5
Is Active: True
BMI: 24.65

Conclusion

4-tuples in C# provide a convenient way to group four related values of different types into a single object. They are particularly useful for returning multiple values from methods and temporarily holding related data without creating a custom class.

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

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements