C# ValueTuple

In c#, ValueTuple is same as Tuple to store the sequence of elements of different data types but the only difference is ValueTuples are the value types (structures) rather than reference types (classes) like Tuple and it will overcome the limitations of Tuple class.

 

The ValueTuple has been introduced in C# 7.0 (.NET Framework 4.7) and it will available in your project if you are using .NET Framework 4.7 or higher, or .NET Standard 2.0 or higher.

 

In case, if you don’t see the ValueType in your project, then you need to install it from the NuGet package manager, for that right click on your project à select Manage NuGet Packages. In NuGet Package Manager window click on the Browse tab, search for System.ValueTuple and install it like as shown below.

 

Add ValueTuple Package Reference in C#

C# Create ValueTuple

In c#, we can create and initialize the ValueTuple by specifying values within the parentheses () like as shown below.

 

var user = (1, "Rohini", "Guntur", 90.5);

 

Or

 

ValueTuple<int, string, string, double> user1 = (1, "Rohini", "Guntur", 90.5);

 

Or

 

(int, string, string, double) user2 = (1, "Rohini", "Guntur", 90.5);

If you observe the above ValueTuple declarations, we created and initialized the values in different ways based on our requirements.

 

As discussed, the Tuple object will directly support only 8 elements, but the ValueTuple will support more than 8 elements.

 

var numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);

C# Access ValueTuple Elements

In c#, we can access the ValueTuple elements same like Tuple object by using Item<positionNumber> properties. For example, the first element can be accessed like Item1, the second element can be accessed like Item2, and so on based on our requirements.

 

Following is the example of accessing the ValueTuple object elements in c#.

 

var user = Tuple.Create(1, "Rohini", "Guntur", 90.5);

var Id = user.Item1; // 1

var name = user.Item2; // Rohini

var location = user.Item3; // Guntur

In c#, we can create a Tuple object even with 1 element using Tuple.Create() method, but to create ValueTuple object it required at least 2 values.

 

// Not a Tuple

var number = (1);

// valid ValueTuple

var numbers = (1, 2);

If you observe the above code, we created a number object with 1 element. If we try to access the number object element, it won’t show that element (Item1) in the properties.

 

C# ValueTuple with Single Element

 

In case, if we try to access numbers object elements, it will show the numbers object elements in the properties list like as shown below.

 

C# ValueTuple Access Elements

C# ValueTuple Example

Following is the example of creating and accessing the ValueTuple object values in c#.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            var user = (1, "Rohini", "Guntur", 90.5);

            Console.WriteLine("*****User Details*****");

            Console.WriteLine("Id:{0}", user.Item1);

            Console.WriteLine("Name:{0}", user.Item2);

            Console.WriteLine("Location:{0}", user.Item3);

            Console.WriteLine("Percentage:{0}", user.Item4);

 

            var numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

            Console.WriteLine("*****Numbers*****");

            Console.WriteLine("{0}", numbers.Item1);

            Console.WriteLine("{0}", numbers.Item10);

            Console.WriteLine("{0}", numbers.Item12);

            Console.ReadLine();

        }

    }

}

When we execute the above program, we will get the result like as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

 

*****Numbers*****

1

10

12

C# Named & Unnamed Tuples

Generally, if we create a Tuple without providing any alternative names for elements like as shown below, then those will called as unnamed tuples.

 

var user = (1, "Rohini", "Guntur", 90.5);

The above tuple object (user) elements can be accessed with default property names like Item1, Item2, and so on because we didn’t provided any name for elements.

If we create a Tuple by assigning the names to elements, then that will be called as named Tuple.

 

In c#, only ValueTuple objects will allow us to provide the names for elements. Following is the example of creating ValueTuple objects with property names.

 

var user = (Id: 1, Name: "Rohini", Location: "Guntur", Percentage: 90.5);

 

Or

 

(int Id, string Name, string Location, double Percentage) user1 = (2, "Suresh", "Hyderabad", 80.0);

The above ValueTuple object elements can be accessed either by using the specified names or with the default property names (Item1, Item2, and so on) like as shown below.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            var user = (Id: 1, Name: "Rohini", Location: "Guntur", Percentage: 90.5);

            Console.WriteLine("*****User Details*****");

            Console.WriteLine("Id:{0}", user.Id);

            Console.WriteLine("Name:{0}", user.Name);

            Console.WriteLine("Location:{0}", user.Location);

            Console.WriteLine("Percentage:{0}", user.Percentage);

 

            var user1 = (Id: 2, Name: "Suresh", Location: "Hyderabad", Percentage: 80.0);

            Console.WriteLine("*****User Details*****");

            Console.WriteLine("Id:{0}", user1.Item1);

            Console.WriteLine("Name:{0}", user1.Item2);

            Console.WriteLine("Location:{0}", user1.Item3);

            Console.WriteLine("Percentage:{0}", user1.Item4);

            Console.ReadLine();

        }

    }

}

If you observe the above example, we created ValueTuple objects (user & user1) and accessing the elements by using names (Id, Name, Location, and Percentage) and with default property names (Item1, Item2, etc.).

 

When we execute the above program, we will get the result like as shown below.

 

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

 

*****User Details*****

Id:2

Name:Suresh

Location:Hyderabad

Percentage:80

C# ValueTuple as Method Parameter

In c#, we can use ValueTuple as a method parameter to send multiple values to the method with a single parameter.

 

Following is the example of using ValueTuple as a method parameter in c#.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            var user = (1, "Rohini", "Guntur", 90.5);

            GetUserDetails(user);

            Console.ReadLine();

        }

        public static void GetUserDetails((int, string, string, double) user)

        {

            Console.WriteLine("*****User Details*****");

            Console.WriteLine("Id:{0}", user.Item1);

            Console.WriteLine("Name:{0}", user.Item2);

            Console.WriteLine("Location:{0}", user.Item3);

            Console.WriteLine("Percentage:{0}", user.Item4);

        }

    }

}

If you observe the above example, we are sending ValueTuple object (user) as a parameter to the GetUserDetails() method.

 

When we execute the above program, we will get the result like as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

By using ValueTuple, we can send multiple values as a single parameter to the method based on our requirements.

C# ValueTuple as Return Type

In c#, we can use ValueTuple as a method return type to return the multiple values without using ref or out parameters.

 

Following is the example of using ValueTuple as the return type of method to return the multiple values in c#.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            var user = GetUserDetails();

            Console.WriteLine("*****User Details*****");

            Console.WriteLine("Id:{0}", user.Item1);

            Console.WriteLine("Name:{0}", user.Item2);

            Console.WriteLine("Location:{0}", user.Item3);

            Console.WriteLine("Percentage:{0}", user.Item4);

            Console.ReadLine();

        }

        public static (int, string, string, double) GetUserDetails()

        {

            var user = (1, "Rohini", "Guntur", 90.5);

            return user;

        }

    }

}

If you observe the above example, we created a method GetUserDetails() with return type as ValueTuple object to return multiple values.

 

When we execute the above program, we will get the result like as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

C# ValueTuple Deconstruction

In c#, we can deconstruct the elements of ValueTuple object and assign to local variables in different ways.

 

We can explicitly specify the type for each element inside of parenthesis () to create a discrete variable for each element in the tuple like as shown below.

 

(int Id, string Name, string Location, double Percentage) = GetUserDetails();

public static (int, string, string, double) GetUserDetails()

{

return (1, "Rohini", "Guntur", 90.5);

}

Another way is to declare the implicitly typed variables for each field in a tuple by using the var keyword outside the parenthesis () like as shown below.

 

var (Id, Name, Location, Percentage) = GetUserDetails();

public static (int, string, string, double) GetUserDetails()

{

return (1, "Rohini", "Guntur", 90.5);

}

We are not allowed to use any specific type outside the parenthesis other than var keyword, even if every field in the tuple has the same type.

 

Following is the example of deconstructing the elements of ValueTuple object.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            var (Id, Name, Location, Percentage) = GetUserDetails();

            Console.WriteLine("*****User Details*****");

            Console.WriteLine("Id:{0}", Id);

            Console.WriteLine("Name:{0}", Name);

            Console.WriteLine("Location:{0}", Location);

            Console.WriteLine("Percentage:{0}", Percentage);

            Console.ReadLine();

        }

        public static (int, string, string, double) GetUserDetails()

        {

            return (1, "Rohini", "Guntur", 90.5);

        }

    }

} 

If you observe the above example, we declared the implicitly typed variables for each field in a tuple by using the var keyword outside the parenthesis ().

 

When we execute the above program, we will get the result like as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

This is how we can use the ValueTuple in our applications based on our requirements. 

 

PREVIOUS

C# Tuple
 

NEXT

LINQ C#  
 

Topics Covered