Open In App

ValueTuple in C#

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:



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:

// 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)    




// C# program to illustrate how to
// create value tuple using the
// ValueTuple constructor.
using System;
 
class GFG {
 
    // Main method
    static public void Main()
    {
 
        // ValueTuple with one element
        ValueTuple<int> ValTpl1 = new ValueTuple<int>(345678);
 
        // ValueTuple with three elements
        ValueTuple<string, string, int> ValTpl2 = new ValueTuple<string,
                                        string, int>("C#", "Java", 586);
 
        // ValueTuple with eight elements
        ValueTuple<int, int, int, int, int, int, int, ValueTuple<int> > ValTpl3 = new ValueTuple<int,
                                  int, int, int, int, int, int, ValueTuple<int> >(45, 67, 65, 34, 34,
                                                                    34, 23, new ValueTuple<int>(90));
    }
}

// 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)




// C# program to create value tuple
// using Create Method
using System;
 
public class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Creating 0-ValueTuple
        // Using Create() Method
        var Valtpl1 = ValueTuple.Create();
 
        // Creating 3-ValueTuple
        // Using Create(T1, T2, T3) Method
        var Valtpl2 = ValueTuple.Create(12, 30, 40, 50);
 
        // Creating 8-ValueTuple
        // Using Create(T1, T2, T3, T4, T5, T6, T7, T8) Method
        var Valtpl3 = ValueTuple.Create(34, "GeeksforGeeks",
                      'g', 'f', 'g', 56.78, 4323, "geeks");
    }
}




// C# program to illustrated named member
using System;
 
public class GFG {
    static public void Main()
    {
        (int age, string Aname, string Lang) author = (23, "Sonia", "C#");
    }
}






// C# program to illustrated named member
using System;
 
public class GFG {
 
    static public void Main()
    {
        var author = (age : 23, Aname
                      : "Sonia", Lang
                      : "C#");
    }
}




// C# program to illustrated UnNamed member
using System;
 
public class GFG {
    static public void Main()
    {
        var author = (20, "Siya", "Ruby");
    }
}




// C# program to illustrated UnNamed member
using System;
 
public class GFG {
    static public void Main()
    {
        ValueTuple<int, string, string> author = (20, "Siya", "Ruby");
    }
}

Accessing ValueTuple members

Here we learn how to access named and unnamed members of the ValueTuples.




// C# program to illustrate how to
// access unnamed members of ValueTuple
using System;
 
public class GFG {
 
    // Main Method
    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);
    }
}




// C# program to illustrate how to access
// named members of ValueTuple
using System;
 
public class GFG {
 
    // Main Method
    static public void Main()
    {
 
        // ValueTuple with three elements
        var library = (Book_id : 2340, Author_name
                       : "Arundhati Roy", Book_name
                       : "The God of Small Things");
 
        // 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);
    }
}

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

Article Tags :
C#