Set 6-item tuple in C#'

A 6-item tuple in C# is a data structure that can hold six values of different or same data types. Tuples are useful when you need to return multiple values from a method or group related data together without creating a separate class.

Syntax

Following is the syntax for creating a 6-item tuple using the Tuple class −

var tuple = new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);

Alternatively, you can use the modern tuple syntax with parentheses −

var tuple = (item1, item2, item3, item4, item5, item6);

Using Traditional Tuple Class

The traditional way to create a 6-item tuple is using the Tuple<> generic class. Items are accessed using Item1, Item2, etc., properties −

using System;

class Demo {
   static void Main() {
      var myTuple = new Tuple<string, string[], int, int, int, int>("electronics",
      new string[] { "shoes", "clothing", "accessories" },
      100,
      250,
      500,
      1000);

      // Accessing tuple items
      Console.WriteLine("Category: " + myTuple.Item1);
      Console.WriteLine("First product: " + myTuple.Item2[0]);
      Console.WriteLine("Price 1: " + myTuple.Item3);
      Console.WriteLine("Price 4: " + myTuple.Item6);
   }
}

The output of the above code is −

Category: electronics
First product: shoes
Price 1: 100
Price 4: 1000

Using Modern Value Tuple Syntax

C# 7.0 introduced a simpler syntax using parentheses with optional named fields −

using System;

class Demo {
   static void Main() {
      // Modern tuple syntax with named fields
      var product = (
         Category: "electronics",
         Subcategories: new string[] { "phones", "laptops", "tablets" },
         MinPrice: 50,
         MaxPrice: 2000,
         DiscountPercent: 15,
         StockCount: 150
      );

      Console.WriteLine("Category: " + product.Category);
      Console.WriteLine("Min Price: " + product.MinPrice);
      Console.WriteLine("Max Price: " + product.MaxPrice);
      Console.WriteLine("Stock: " + product.StockCount);
      Console.WriteLine("Subcategories: " + string.Join(", ", product.Subcategories));
   }
}

The output of the above code is −

Category: electronics
Min Price: 50
Max Price: 2000
Stock: 150
Subcategories: phones, laptops, tablets

Returning 6-Item Tuple from Method

Tuples are commonly used to return multiple values from methods −

using System;

class Calculator {
   public static (int sum, int difference, int product, int quotient, int remainder, double average) 
   Calculate(int a, int b) {
      return (
         sum: a + b,
         difference: a - b,
         product: a * b,
         quotient: a / b,
         remainder: a % b,
         average: (a + b) / 2.0
      );
   }

   static void Main() {
      var results = Calculate(20, 4);
      
      Console.WriteLine("Sum: " + results.sum);
      Console.WriteLine("Difference: " + results.difference);
      Console.WriteLine("Product: " + results.product);
      Console.WriteLine("Quotient: " + results.quotient);
      Console.WriteLine("Remainder: " + results.remainder);
      Console.WriteLine("Average: " + results.average);
   }
}

The output of the above code is −

Sum: 24
Difference: 16
Product: 80
Quotient: 5
Remainder: 0
Average: 12

Comparison of Tuple Approaches

Traditional Tuple<> Modern Value Tuple
Uses Item1, Item2, etc. Supports named fields
Reference type (heap allocated) Value type (stack allocated)
Available since C# 4.0 Available since C# 7.0
Less readable with generic names More readable with descriptive names

Conclusion

6-item tuples in C# provide a convenient way to group six related values together. The modern value tuple syntax with named fields offers better readability and performance compared to the traditional Tuple<> class, making it the preferred approach for new code.

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

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements