Open In App

C# ValueTuple <T1,T2,T3,T4> Struct

Last Updated : 21 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

ValueTuple <T1, T2, T3, T4> struct in C# is part of the System namespace and it is used to create a tuple that stores quadruple or 4-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTuple <T1, T2, T3, T4> struct is a value type and mutable means the value of its elements can be modified.

Key Features

  • Elements: ValueTuple <T1,T2,T3,T4> contains four elements referred to as Item<ElementNumber>.
  • Mutable: The value of the Item(fields) can be changed after the tuple is created.
  • Type-Safe: The type of field is specified when the tuple is created, ensuring type safety.
  • Implements Interfaces: ValueTuple<T1, T2, T3, T4> implements several interfaces such as:
    • IStructuralComparable
    • IStructuralEquatable
    • IComparable<ValueTuple<T1, T2, T3, T4>>
    • IEquatable<ValueTuple<T1, T2, T3, T4>>
    • ITuple: This makes it useful in various comparison and equality scenarios.

Constructor

ValueTuple<T1, T2, T3, T4>(T1, T2, T3, T4

Fields

The constructor for ValueTuple <T1, T2, T3, T4> initializes the tuple with four values. It is not a property, it is a field that can be accessed by ValueTuple<ElementNumber>

  • Item1: Gets the value of the current ValueTuple<T1, T2, T3, T4> instance’s first element.
  • Item2: Gets the value of the current ValueTuple<T1, T2, T3, T4> instance’s second element.
  • Item3: Gets the value of the current ValueTuple<T1, T2, T3, T4> instance’s third element.
  • Item4: Gets the value of the current ValueTuple<T1, T2, T3, T4> instance’s fourth element.

Example: Demonstration of access to the ValueTuple<T1, T2, T3, T4> element in C#.

C#
// Accessing the elements of
// ValueTuple<T1, T2, T3, T4>
using System;

class Geeks
{
	static public void Main()
	{
		// Creating a value tuple using create method
		var library = ValueTuple.Create(3456, "The Guide",
		"R. K. Narayan", 1958);

		// Display the element of the given value tuple
		Console.WriteLine("Book Details: ");
		Console.WriteLine("Book Id: {0}", library.Item1);
		Console.WriteLine("Book Name: {0}", library.Item2);
		Console.WriteLine("Author Name: {0}", library.Item3);
		Console.WriteLine("Publication date: {0}", library.Item4);
	}
}

Output
Book Details: 
Book Id: 3456
Book Name: The Guide
Author Name: R. K. Narayan
Publication date: 1958

Explanation: In the above example, it creates a ValueTuple with a four elements of different types and prints each element by field Item<elementNumber>.

Methods

Method

Description

CompareTo(ValueTuple)

It compares the current ValueTuple<T1> instance to a specified ValueTuple<T1> instance.

Equals(Object)

It returns a value that indicates whether the current ValueTuple<T1> instance equals a specified object.

Equals(ValueTuple<T1>)

It returns a value that indicates whether the current ValueTuple<T1> instance equals a specified ValueTuple<T1> instance.

GetHashCode()

It calculates the hash code for the current ValueTuple<T1> instance.

ToString()

It returns a string that represents the value of this ValueTuple<T1> instance.

Example of Equals() Method in ValueTuple

C#
// Check if value tuples are Equal
using System;

class Geeks
{
	static public void Main()
	{
		// Creating 4-ValueTuple
      	// Using Create method
		var T1 = ValueTuple.Create(346, 784, 45, 22);
		var T2 = ValueTuple.Create(346, 7743, 56, 22);

		// Check if both the value
		// tuples are equal or not
		if (T1.Equals(T2))
			Console.WriteLine("Code is correct...!!");
		else
			Console.WriteLine("Incorrect Code...!!");
	}
}

Output
Incorrect Code...!!



Next Article
Article Tags :

Similar Reads