Valuetuple In C#

ValueTuple is a recent addition introduced in C# 7.0, serving as a representation of the Tuple value type. It comes pre-installed in .NET Framework 4.7 and its later editions. This feature allows us to store a collection of data containing multiple values that are not necessarily related. ValueTuple has the capacity to accommodate elements from 0 to 8, supporting different data types within the same structure. Additionally, it permits the storage of duplicate items within its contents.

Need of ValueTuple

In C#, we already have Tuples for storing multiple values, but Tuples have several constraints that ValueTuple overcomes. Alternatively, ValueTuple is an enhanced version of Tuples in C#. It gets over Tuples's drawbacks as follows:

  • Tuple is of the reference type, whereas ValueTuple is of the value type.
  • Tuple lacks naming conventions, but ValueTuple has strong naming standards.
  • Tuples do not enable us to build a tuple with zero components, whereas ValueTuple allows us to create a tuple having zero elements.
  • Tuple outperforms ValueTuple in terms of performance. ValueTuple is useful because it provides a lightweight technique for returning numerous values from existing methods. Furthermore, the syntax of ValueTuple has been more optimized than that of Tuples.
  • ValueTuple gives us additional access to the elements of value tuples by using deconstruction and the keyword . However, Tuple is unable to offer the keyword and the deconstruction concepts.
  • Components such as item1 and item2 are fields in ValueTuple. However, in Tuple, they contains properties.
  • Fields in ValueTuple are modifiable. However, fields in Tuple are only available for reading.
  • Making a ValueTuple

Unlike Tuples, ValueTuples offer a convenient approach to generate and set up ValueTuples. ValueTuples can be formed through three different methods:

  1. Utilizing Constructors: ValueTuples can be constructed by employing the ValueTuple Struct constructor, enabling the storage of components from one to eight along with their respective types.

Syntax:

It has the following syntax:

Example

//Constructor
ValueTuple<Tup1>(Tup1)
//The two elements are created
ValueTuple<Tup1, Tup2>(Tup1, Tup2)
.
.
.
//The other elements are created
ValueTuple<Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, TRest>(Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, TRest)

Example:

Let's consider an instance to demonstrate the process of constructing a ValueTuple in C# using a constructor.

Example

using System;

class ValueTupleExample {

  // Main method
  static public void Main()
  {

    // The tuple having one element
    ValueTuple<int> ValTpl1 = new ValueTuple<int>(3456879);

    // The tuple having three elements
    ValueTuple<string, string, int> ValTpl2 = new ValueTuple<string, string, int>("C#", ".Net", 5906);

    // The value tuple consisting of 8 elements
    ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> ValTpl3 = new ValueTuple<int,
                int, int, int, int, int, int, ValueTuple<int>>(67, 64, 65, 32, 39, 37, 21, new ValueTuple<int>(96));

    // Printing out the content of tuples
    Console.WriteLine($"ValTpl1: {ValTpl1.Item1}");

    Console.WriteLine($"ValTpl2: {ValTpl2.Item1}, {ValTpl2.Item2}, {ValTpl2.Item3}");

    Console.WriteLine($"ValTpl3: {ValTpl3.Item1}, {ValTpl3.Item2}, {ValTpl3.Item3}, {ValTpl3.Item4}, {ValTpl3.Item5}, {ValTpl3.Item6}, {ValTpl3.Item7}, {ValTpl3.Rest.Item1}");
  }
}

Output:

Output

ValTpl1: 3456879
ValTpl2: C#, .Net, 5906
ValTpl3: 67, 64, 65, 32, 39, 37, 21, 96
  1. Using the create Method:

When we utilize the ValueTuple<T> struct's constructor to create a value tuple, it is necessary to specify the type of each element within the tuple, leading to increased complexity in our code. To address this, C# introduces an alternative ValueTuple struct that offers static techniques for generating value tuple instances without the need to explicitly define the type of each element.

Syntax:

It has the following syntax:

Example

// An empty tuple creation
Create();

// the method for creating 1 tuple
Create<Tup1>(Tup1)
.
.
.
// Method which is used for the creation of 8 tuples
Create<Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, TRest>(Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, Tup8)

Example:

Let's consider a scenario to demonstrate the process of generating a ValueTuple by utilizing the Create method in C#.

Example

using System;
public class CreateMethod{
  // Main method
  static public void Main()
  {
    // the create() method for tuple creation
    var Valtupl1s = ValueTuple.Create();

    // the Create(t1,t2,t3) method for creating 3 tuples
  
    var Valtupl2s = ValueTuple.Create(15, 34, 44, 55);

    // Making an 8-ValueTuple // Using the Create(Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, Tup8) Method
    var Valtupl3s = ValueTuple.Create(39, "Programming",
          'j', 'f', 'j', 59.78, 431, "java");

    // display of the tuple elements
    Console.WriteLine($"Valtupl1s: {Valtupl1s}");
    Console.WriteLine($"Valtupl2s: {Valtupl2s.Item1}, {Valtupl2s.Item2}, {Valtupl2s.Item3}, {Valtupl2s.Item4}");
    Console.WriteLine($"Valtupl3s: {Valtupl3s.Item1}, {Valtupl3s.Item2}, {Valtupl3s.Item3}, {Valtupl3s.Item4}, {Valtupl3s.Item5}, {Valtupl3s.Item6}, {Valtupl3s.Item7}, {Valtupl3s.Rest.Item1}");
  }
}

Output:

Output

Valtupl1s: ()
Valtupl2s: 15, 34, 44, 55
Valtupl3s: 39, Programming, j, f, j, 59.78, 431, java
  1. Using parenthesis:

It is the simplest method to generate ValueTuples, with the elements being placed in between. The elements are stored in two different manners:

Named Member: ValueTuple allows us to form a tuple where each element is assigned a specific name. This feature enables us to access individual elements using their designated names, enhancing the readability and manageability of our code. It is important to note that we can name members on either the left or right side of the tuple, but not on both sides simultaneously. In case names are provided for both sides, the names on the left take precedence over those on the right. This behavior is illustrated in the example below:

Example 1:

Example

using System;
public class Namespace {
	static public void Main()
	{
		(int ages, string Anames, string Language) authors = (232, "Sony", "C#");
	}
}

Unnamed elements: These are elements within ValueTuples that do not possess specific names. They are generated without any assigned name.

Example 2:

Example

using System;
public class Names{
	static public void Main()
	{
		var authors = (22, "Sriya", "Rohan");
	}
}

Input Required

This code uses input(). Please provide values below: