C# ValueTuple
ValueTuple is a structure introduced in C# 7.0 which represents the value type. Already included in .NET Framework 4.7 or higher version. It allows us to store a data set that 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. We can also store duplicate elements in the value tuple.
Need of Value Tuple
We already have Tuples in C# which is used to store multiple values, but have some limitations, these limitations are fixed in ValueTuple. ValueTuple is an improved version of Tuples. It overcomes the following limitations of Tuples:
- Tuple is of reference type, but ValueTuple is of the value type.
- Tuple does not provide naming conventions, but ValueTuple provides strong naming conventions.
- Tuples can't have zero components, but ValueTuple can have zero elements.
- ValueTuple provides a lightweight mechanism for returning multiple values from the existing methods.
- The syntax of ValueTuple is more optimized than Tuples.
- ValueTuple allows flexible element access through deconstruction and the _ keyword, unlike Tuple, which lacks these features.
- 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
We can create ValueTuples by using the following ways:
- Using Constructor
- Using Create Method
- Using parenthesis()
1. Using Constructor
We can create ValueTuple by using the constructor provided by the ValueTuple<T> Struct, where we 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 create ValueTuple
// using the constructor
using System;
class Geeks
{
static public void Main()
{
// ValueTuple with one element
ValueTuple<int> vt1 = new ValueTuple<int>(345678);
// ValueTuple with three elements
ValueTuple<string, string, int> vt2 = new ValueTuple<string,
string, int>("C#", "Java", 586);
// ValueTuple with eight elements
ValueTuple<int, int, int, int, int, int, int,
ValueTuple<int>> vt3 = new ValueTuple<int,
int, int, int, int, int, int, ValueTuple<int>>(45, 67, 65, 34, 34,
34, 23, new ValueTuple<int>(90));
// Accessing. values
Console.WriteLine("first element of ValueTuple: " + vt1.Item1);
Console.WriteLine("ValueTuple with three elements: "
+ vt2.Item1 + " " + vt2.Item2 + " " + vt2.Item3);
Console.WriteLine("elements of ValueTuple with eight items: "
+ vt3.Item1 + " " + vt3.Item2);
}
}
Output
first element of ValueTuple: 345678 ValueTuple with three elements: C# Java 586 elements of ValueTuple with eight items: 45 67
2. Using the Create Method
The Create method is a simpler way to create ValueTuple objects without explicitly specifying the types of the tuple elements.
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 ValueTuples
// using Create method
using System;
public class Geeks
{
static public void Main()
{
// Creating 0-ValueTuple
// Using Create() Method
// var V1 = ValueTuple.Create();
// Creating 3-ValueTuple
// Using Create(T1, T2, T3) Method
var V2 = ValueTuple.Create(12, 30, 40, 50);
// Creating 8-ValueTuple
// Using Create(T1, T2, T3, T4, T5, T6, T7, T8) Method
var V3 = ValueTuple.Create(34, "GeeksforGeeks",
'g', 'f', 'g', 56.78, 4323, "geeks");
// Accessing values of V1
Console.WriteLine("V1 is an empty ValueTuple");
// Accessing values of V2
Console.WriteLine("V2 Item1: " + V2.Item1);
Console.WriteLine("V2 Item3: " + V2.Item3);
// Accessing values of V3
Console.WriteLine("V3 Item2: " + V3.Item2);
Console.WriteLine("V3 Item6: " + V3.Item6);
}
}
Output
V1 is an empty ValueTuple V2 Item1: 12 V2 Item3: 40 V3 Item2: GeeksforGeeks V3 Item6: 56.78
3. Using Parentheses
It is the simplest form for creating ValueTuples using parenthesis "()" and the elements are placed in between them. The elements are stored in 2 different ways:
- Named Member
- UnNamed Member
3.1 Named Member
ValueTuple allows us to create a tuple in which each component is allowed to have a its own name. So we can access that component with the help of their name. It makes the program more readable. We can assign the names to the members on either the left-hand side or right-hand, but not both sides. If we assigned names to both sides, then the left-hand side has precedence over the right-hand side.
Example 1: Named Member
// C# program to illustrate
// Named Members in a ValueTuple
using System;
public class Geeks
{
static public void Main()
{
(int age, string name, string Lang) author = (23, "Geek", "C#");
Console.WriteLine("Author's age: " + author.age);
Console.WriteLine("Author's name: " + author.name);
Console.WriteLine("Author's language: " + author.Lang);
}
}
Output
Author's age: 23 Author's name: Geek Author's language: C#
Example 2: Named Members Using var
// C# program to illustrate
// Named Members using var
using System;
public class Geeks
{
static public void Main()
{
var author = (age: 23, name
: "Geek", Lang
: "C#");
Console.WriteLine("Author Name: " + author.name);
Console.WriteLine("Author Age: " + author.age);
Console.WriteLine("Author Language: " + author.Lang);
}
}
Output
Author Name: Geek Author Age: 23 Author Language: C#
3.2 Unnamed Members
In ValueTuples, the unnamed members are those members who do not have names. They are simply created without any name.
Example:
// C# program to illustrate
// Unnamed Members in a ValueTuple
using System;
public class Geeks
{
static public void Main()
{
var author = (20, "Geek", "C#");
Console.WriteLine("Author Age: " + author.Item1);
Console.WriteLine("Author Name: " + author.Item2);
Console.WriteLine("Author Language: " + author.Item3);
}
}
Output
Author Age: 20 Author Name: Geek Author Language: C#
Accessing ValueTuple members
1. 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:
// Accessing unnamed ValueTuple members
using System;
public class Geeks
{
static public void Main()
{
// ValueTuple with three elements
var author = (20, "Siya", "Ruby");
// Accessing the ValueTuple
// Using default Item property
Console.WriteLine("Age:" + author.Item1);
Console.WriteLine("Name:" + author.Item2);
Console.WriteLine("Language:" + author.Item3);
}
}
Output
Age:20 Name:Siya Language:Ruby
2. Accessing Named Members
In ValueTuples, the named members are used according to their names. 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:
// Accessing named ValueTuple members
using System;
public class Geeks
{
static public void Main()
{
// ValueTuple with three elements
var library = (Book_id: 2340, Author_name
: "Geek", Book_name
: "C# tutorials");
// Accessing the ValueTuple
// according to their names
Console.WriteLine("Book Id: {0}", library.Book_id);
Console.WriteLine("Author Name: {0}", library.Author_name);
Console.WriteLine("Book Name: {0}", library.Book_name);
}
}
Output
Book Id: 2340 Author Name: Geek Book Name: C# tutorials
Returning ValueTuple from a Method
In C#, we 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:
// Demonstration of method returning a ValueTuple
using System;
public class Geeks
{
// This method returns the tourist details
static (int, string, string) TouristDetails()
{
return (357, "Geek", "USA");
}
static public void Main()
{
// Store the data provided by
// the TouristDetails method
var (Tourist_Id, Tourist_Name, Country)
= TouristDetails();
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: 357 Tourist Name: Geek Country: USA