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:

  • 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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, "GeeksforGeks"
                          'g', 'f', 'g', 56.78, 4323, "geeks");
        }
    }

    chevron_right

    
    

  • 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:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // 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#");
          }
      }

      chevron_right

      
      

      Example 2:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

    • 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:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Example 2:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

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:


    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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}");
    }
}

chevron_right


Output:

Tourist Details: 
Tourist Id: 384645
Tourist Name: Sophite
Country: USA


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.