What Is A Tuple And Value Tuple In C#

In this article, we will discuss about tuple and how value tuple makes it easy to use in C#.

So, as we know tuple is one of the good features of C#. Tuple class was introduced in .Net Framework 4. Tuple is a data structure and can contain any kind of data types in a sequential way.

Just to recall, before .Net introduced tuple, we had below ways to return multiple values from a method

  • Out parameters
  • Class creation with property and return
  • Using Struct

Create tuple in C#

Tuple<int, string, bool> user = new Tuple<int, string, bool>(1, "C# Developer", true);

Above tuple hold three different types of data. In this way, we specify the type of each element which is little difficult to write. Hence C# provides a Static helper class that can return type without specifying the element’s type. Hence, above code can be written as shown below.

var user = Tuple.Create(1, "C# Developer", true);

Accessing Tuple Items

Tuple items can be accessed as below

var user = Tuple.Create(1, "C# Developer", true);
Console.WriteLine("User id - " + user.Item1);
Console.WriteLine("User Name - " + user.Item2);
Console.WriteLine("User is Active - " + user.Item3);

Example

Create a simple .net Console application and update main method with below code sample and run the project.

static void Main(string[] args) {
    Console.WriteLine("Hello World!");
    var user = Tuple.Create(1, "C# Developer", true);
    Console.WriteLine("User id - " + user.Item1);
    Console.WriteLine("User Name - " + user.Item2);
    Console.WriteLine("User is Active - " + user.Item3);
    Console.ReadKey();
}

What is a tuple and Value tuple in C# ?

Use Tuple with method params

Let’s take same example and see, how we can pass tuple as a parameter in method. Update your static method with below code and run the project. We will see same output.

static void Main(string[] args) {
    Console.WriteLine("Hello World!");
    var user = Tuple.Create(1, "C# Developer", true);
    DisplayUserDetails(user);
    Console.ReadKey();
}
private static void DisplayUserDetails(Tuple < int, string, bool > user) {
    Console.WriteLine("User id - " + user.Item1);
    Console.WriteLine("User Name - " + user.Item2);
    Console.WriteLine("User is Active - " + user.Item3);
}

What is a tuple and Value tuple in C# ?

Use Tuple as Return Type

Now we will see how we can return multiple records from a method using tuple and then we will display its data. Modify your code as below and run the project, you will get same output window.

static void Main(string[] args) {
    Console.WriteLine("Hello World!");
    var user = GetUserDetails();
    DisplayUserDetails(user);
    Console.ReadKey();
}
// Here we are using tuple as return type which return multiple data from a method
private static Tuple < int, string, bool > GetUserDetails() {
    return Tuple.Create(1, "C# Developer", true);
}
// here we are using tuple as method parameter
private static void DisplayUserDetails(Tuple < int, string, bool > user) {
    Console.WriteLine("User id - " + user.Item1);
    Console.WriteLine("User Name - " + user.Item2);
    Console.WriteLine("User is Active - " + user.Item3);
}

What is a tuple and Value tuple in C# ?

C# - ValueTuple

Value tuple is introduced in C# 7.0 (Framework 4.7) which is value type representation of tuple. Value tuple makes tuple initialization and its uses very easy.

It can be created and initialized using parentheses () and specifying the values in it.

var valueTuple = (1, "C# Developer", true);
//above declaration is equivalent to below.
var valueTuple = Tuple.Create(1, "C# Developer", true);

Declare and use Value tuple

static void Main(string[] args) {
    Console.WriteLine("Value tuple example !");
    (int, string, bool) user = (1, "C# Developer", true);
    Console.WriteLine("User id - " + user.Item1);
    Console.WriteLine("User Name - " + user.Item2);
    Console.WriteLine("User is Active - " + user.Item3);
}

What is a tuple and Value tuple in C# ?

Point to note in above code that we have not used var keyword at initialization instead we declare each element type.

Use Value Tuple with method params

Value tuple can also be used as method params like tuple.

static void Main(string[] args) {
    Console.WriteLine("Value tuple example !");
    DisplayUserDetails((1, "C# Developer", true));
    Console.ReadKey();
}
static void DisplayUserDetails((int, string, bool) user) {
    Console.WriteLine("User id - " + user.Item1);
    Console.WriteLine("User Name - " + user.Item2);
    Console.WriteLine("User is Active - " + user.Item3);
}

Use Value Tuple as Return Type

Value tuple can also be used as return type from method

static void Main(string[] args) {
    Console.WriteLine("Value tuple example !");
    var userInfo = GetUserDetails();
    DisplayUserDetails(userInfo);
    Console.ReadKey();
}
static(int Id, string CourseName, bool isActive) GetUserDetails() {
    return (Id: 1, CourseName: "C# Developer", isActive: true);
}

That’s it with this article. I have explained about tuple feature and how value tuple make it easy with declaration, initialization, and its use.

Next Recommended Reading C# 7.0 New Feature - Tuples In C# 7