C# - ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> Struct
Last Updated :
11 Jul, 2025
<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> struct in C# is part of the System namespace and is used to create n-ValueTuple where n = 8 or greater than 8. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTuple struct is a value type and mutable means the value of its elements can be modified. Some key features of tuples are mentioned below:
- Mutable: The value of the Item(fields) can be changed after the tuple is created.
- Type-Safe: The field is specified when the tuple is created, ensuring type safety.
- Implements Interfaces: <ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>, implements several interfaces such as:
- IStructuralComparable
- IStructuralEquatable
- IComparable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>
- IEquatable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>
- ITuple: This makes it useful in various comparison and equality scenarios.
Constructor
// Initializes a new ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)
Fields
The constructor of the ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> struct initializes the tuple with seven values, Including the optional TRest that can represent additional elements, Accessed via fields ValueTuple<ElementNumber>.
- Item1: Retrieves the 1st element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item2: Retrieves the 2nd element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item3: Retrieves the 3rd element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item4: Retrieves the 4th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item5: Retrieves the 5th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item6: Retrieves the 6th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item7: Retrieves the 7th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- TRest: Any generic value tuple instance that defines the types of the tuple’s remaining elements.
Example:
C#
// Accessing the element of ValueTuple<T1,
// T2, T3, T4, T5, T6, T7, TRest>
using System;
class Geeks
{
static public void Main()
{
// Creating a value tuple using Create method
var lib = ValueTuple.Create(3, "C#", "Geek",
2025, "Programming", "English", "India",
ValueTuple.Create("Java", "Javascript",
"Kotlin"));
// Display the element of the value-tuple
Console.WriteLine("Details: ");
Console.WriteLine("Book Id: {0}", lib.Item1);
Console.WriteLine("Book Name: {0}", lib.Item2);
Console.WriteLine("Author Name: {0}", lib.Item3);
Console.WriteLine("Publication date: {0}", lib.Item4);
Console.WriteLine("Gener: {0}", lib.Item5);
Console.WriteLine("Language: {0}", lib.Item6);
Console.WriteLine("Country: {0}", lib.Item7);
Console.WriteLine("Other Books Details: " + lib.Rest);
}
}
OutputDetails:
Book Id: 3
Book Name: C#
Author Name: Geek
Publication date: 2025
Gener: Programming
Language: English
Country: India
Other Books Details: ((Java, Javascript, Kotlin))
Explanation: In the above example, we create a ValueTuple with seven elements and one nested valueTuple of different types and print each element by field Item<elementNumber> and nested valuetuple using ValutupleObject.Rest(Instance).
Methods
Methods | Description |
---|
CompareTo(ValueTuple) | Compares two tuples element by element and returns (0,1,-1) value based on their order. |
Equals(Object) | Check the Object equal to the current ValueTuple instance, return true if equals else return false |
Equals(ValueTuple) | Checks if two ValueTuple instances are equal by comparing their elements, return true if equals else return false |
GetHashCode() | Calculates the hash code for the current ValueTuple instance. |
ToString() | Returns a string that represents the value of current ValueTuple instance. |
Example:
C#
// Demonstration of Equals() Method in ValueTuple
using System;
class Geeks
{
static public void Main()
{
// Creating 8-ValueTuple using Create method
var T1 = ValueTuple.Create(1, 2, 3, 4, 5, 6, 7 ,8);
var T2 = ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
// Check if both the value
// tuples are equal or not
if (T1.Equals(T2)) {
Console.WriteLine("Tuples Matched...!!");
}
else {
Console.WriteLine("Tuples Not Matched...!!");
}
}
}
OutputTuples Matched...!!
Similar Reads
Introduction
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers