ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It is already included in .NET Framework 4.7 or higher version. It allows you to store a data set which contains multiple values that may or may not be related to each other. It can store elements starting from 0 to 8 and can store elements of different types. You can also store duplicate elements in value tuple.
Why we need ValueTuple?
We already have Tuples in C# which is used to store multiple values, but Tuples have some limitation, these limitations are fixed in ValueTuple. Or we can say that ValueTuple is an improved version of Tuples in C#. It overcomes the following limitations of Tuples:
- Tuple is of reference type, but ValueTuple is of value type.
- Tuple does not provide naming conventions, but ValueTuple provide strong naming conventions.
- In Tuples you are not allowed to create a tuple with zero component, but in ValueTuple you are allowed to create a tuple with zero elements.
- The performance of ValueTuple is better than Tuple. Because ValueTuple provides a lightweight mechanism for returning multiple values from the existing methods. And the syntax of ValueTuple is more optimized than Tuples.
- ValueTuple provides more flexibility for accessing the elements of the value tuples by using deconstruction and the _ keyword. But Tuple cannot provide the concept of deconstruction and the _ keyword.
- In ValueTuple the members such as item1 and item2 are fields. But in Tuple, they are properties.
- In ValueTuple fields are mutable. But in Tuple, fields are read-only.
Creating a ValueTuple
Unlike Tuple, ValueTuples provides an easy mechanism to create and initialize the ValueTuples. You can create ValueTuples by using the following 3 ways:
- Using Constructor: You can create ValueTuple by using the constructor provided by the ValueTuple<T> Struct. Where you can store elements starting from one to eight with their type.
Syntax:
// Constructor for creating one element ValueTuple<T1>(T1) // Constructor for creating two elements ValueTuple<T1, T2>(T1, T2) . . . // Constructor for creating eight elements ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)
Example:
// C# program to illustrate how to// create value tuple using the// ValueTuple constructor.usingSystem;classGFG {// Main methodstaticpublicvoidMain(){// ValueTuple with one elementValueTuple<int> ValTpl1 =newValueTuple<int>(345678);// ValueTuple with three elementsValueTuple<string,string,int> ValTpl2 =newValueTuple<string,string,int>("C#","Java", 586);// ValueTuple with eight elementsValueTuple<int,int,int,int,int,int,int, ValueTuple<int> > ValTpl3 =newValueTuple<int,int,int,int,int,int,int, ValueTuple<int> >(45, 67, 65, 34, 34,34, 23,newValueTuple<int>(90));}}chevron_rightfilter_none - Using Create Method: When we use the constructor of ValueTuple<T> struct to create a value tuple we need to provide the type of each element stored in the value tuple which makes your code cumbersome. So, C# provides another ValueTuple struct which contains the static methods for creating value tuple object without providing the type of each element.
Syntax:
// Method for creating an empty value tuple Create(); // Method for creating 1-ValueTuple Create<T1>(T1) . . . // Method for creating 8-ValueTuple Create<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, T8)
Example:
// C# program to create value tuple// using Create MethodusingSystem;publicclassGFG {// Main methodstaticpublicvoidMain(){// Creating 0-ValueTuple// Using Create() MethodvarValtpl1 = ValueTuple.Create();// Creating 3-ValueTuple// Using Create(T1, T2, T3) MethodvarValtpl2 = ValueTuple.Create(12, 30, 40, 50);// Creating 8-ValueTuple// Using Create(T1, T2, T3, T4, T5, T6, T7, T8) MethodvarValtpl3 = ValueTuple.Create(34,"GeeksforGeks",'g','f','g', 56.78, 4323,"geeks");}}chevron_rightfilter_none - Using parenthesis(): It is the simplest form for creating ValueTuples using parenthesis() and the elements are placed in between them. And the elements are stored in 2 different ways:
- Named Member: ValueTuple allows you to create a tuple in which each component is allowed to have their own name. So that you can access that component with the help of their name. It makes your program more readable and easy to remember. You can assign the names to the members either left-hand side or right-hand, but not both sides. If you assigned names to both sides, then the left-hand side has precedence over the right-hand side As shown below:
Example 1:
// C# program to illustrated named memberusingSystem;publicclassGFG {staticpublicvoidMain(){(intage,stringAname,stringLang) author = (23,"Sonia","C#");}}chevron_rightfilter_noneExample 2:
// C# program to illustrated named memberusingSystem;publicclassGFG {staticpublicvoidMain(){varauthor = (age : 23, Aname:"Sonia", Lang:"C#");}}chevron_rightfilter_none - UnNamed Member: In ValueTuples, the unnamed members are those members which does not have names. They are simply created without any name. As shown below:
Example 1:
// C# program to illustrated UnNamed memberusingSystem;publicclassGFG {staticpublicvoidMain(){varauthor = (20,"Siya","Ruby");}}chevron_rightfilter_noneExample 2:
// C# program to illustrated UnNamed memberusingSystem;publicclassGFG {staticpublicvoidMain(){ValueTuple<int,string,string> author = (20,"Siya","Ruby");}}chevron_rightfilter_none
- Named Member: ValueTuple allows you to create a tuple in which each component is allowed to have their own name. So that you can access that component with the help of their name. It makes your program more readable and easy to remember. You can assign the names to the members either left-hand side or right-hand, but not both sides. If you assigned names to both sides, then the left-hand side has precedence over the right-hand side As shown below:
Accessing ValueTuple members
Here we learn how to access named and unnamed members of the ValueTuples.
- Accessing unnamed members: In ValueTuple, unnamed members are accessible by using the default item property names like Item1, Item2, Item3, etc. As shown in the below example:
Example:
// C# program to illustrate how to// access unnamed members of ValueTupleusingSystem;publicclassGFG {// Main MethodstaticpublicvoidMain(){// ValueTuple with three elementsvarauthor = (20,"Siya","Ruby");// Accessing the ValueTuple// Using default Item propertyConsole.WriteLine("Age:"+ author.Item1);Console.WriteLine("Name:"+ author.Item2);Console.WriteLine("Language:"+ author.Item3);}}chevron_rightfilter_none - Accessing Named members: In ValueTuples, the named members are used according to their name. There is no need to access these named members with default item property. As shown in the below example, the ValueTuple contains three elements, i.e, Book_id, Author_name, and Book_name. And we directly access these elements according to their names.
Example:
// C# program to illustrate how to access// named members of ValueTupleusingSystem;publicclassGFG {// Main MethodstaticpublicvoidMain(){// ValueTuple with three elementsvarlibrary = (Book_id : 2340, Author_name:"Arundhati Roy", Book_name:"The God of Small Things");// Accessing the ValueTuple// according to their namesConsole.WriteLine("Book Id: {0}", library.Book_id);Console.WriteLine("Author Name: {0}", library.Author_name);Console.WriteLine("Book Name: {0}", library.Book_name);}}chevron_rightfilter_noneOutput:
Book Id: 2340 Author Name: Arundhati Roy Book Name: The God of Small Things
Returning ValueTuple
In C#, you are allowed to return a ValueTuple from a method. As shown in the below example, the TouristDetails method returns a ValueTuple with 3 elements:
Example:
// C# program to illustrate how a // method return ValueTuple using System; public class GFG { // This method returns the tourist details static(int, string, string) TouristDetails() { return (384645, "Sophite", "USA"); } // Main method static public void Main() { // Store the data provided by the TouristDetails method var(Tourist_Id, Tourist_Name, Country) = TouristDetails(); // Display data Console.WriteLine("Tourist Details: "); Console.WriteLine($ "Tourist Id: {Tourist_Id}"); Console.WriteLine($ "Tourist Name: {Tourist_Name}"); Console.WriteLine($ "Country: {Country}"); } } |
Output:
Tourist Details: Tourist Id: 384645 Tourist Name: Sophite Country: USA
Recommended Posts:
- C# | ValueTuple <T1,T2,T3,T4,T5,T6,T7> Struct
- Checking if two ValueTuple<T1,T2,T3,T4> are equal or not in C#
- Check if ValueTuple instances are equal in C#
- How to create 1-ValueTuple in C#?
- How to create 2-ValueTuple in C#?
- How to create 4-ValueTuple in C#?
- How to create 3-ValueTuple in C#?
- How to create 5-ValueTuple in C#?
- How to create 6-ValueTuple in C#?
- How to create 7-ValueTuple in C#?
- How to create 8-ValueTuple in C#?
- How to compare two ValueTuple in C#?
- Getting the hash code of the ValueTuple in C#
- ValueTuple Struct in C#
- How to get Second Element of the ValueTuple in C#?
- How to get fifth Element of the ValueTuple in C#?
- How to get First Element of the ValueTuple in C#?
- How to get third Element of the ValueTuple in C#?
- How to get fourth Element of the ValueTuple in C#?
- How to get sixth Element of the ValueTuple in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

