How to define Tuple in C# | New feature in C# 7

C# 7 has a cool feature called – Tuple. In this post, we will learn what is a tuple in c# and how to define tuple in c#.

C# is one of the best object-oriented programming languages not just because it is easy to work but C# is the best because it is immensely powerful, versatile and has lots of excellent features, and the most important, C# is getting updated regularly.

The seventh major version of C# (called C# 7) was released back in March 2017 along with the Visual Studio 2017. And one of the biggest features of C# was Tuples

What is Tuple in C# –

Here is the definition of tuple in c# –

A tuple is a special data structure that provides concise syntax to work with a group of similar or different data in an amazingly simple manner.

Since Tuples handle a group of data as a one so we can use it at many places to do some exciting work in our program.

Here are some of the use-cases of using a tuple –

  • Return multiple value from a method (function)
  • Pass multiple values in a single parameter
  • Can be useful in LINQ queries in place of Anonymous types
  • And lots more.

How to define Tuple in C# –

Declaring a tuple in C# is quite simple. Tuple also follows the same style of declaring other variables.

There are 2 ways to declare a Tuple –

Declare Tuple using Tuple class –

A Tuple is a class available in C# and can be used to declare a tuple –

Tuple<string, int> employeeTuple;

Remember, you can pass any type to this Tuple.

If you are using Tuple word to work with Tuples, then let’s have a look on the Tuple class.

Create method in Tuple

Just focus on the above image. Here you can see Eight overloaded versions of Create method in Tuple. It means you can have a maximum of eight types if you are using Tuple work to declare the Tuples.

Let’s have a look on few more examples of declaring the Tuples using Tuple keyword.

Tuple<int> myTuple1;
Tuple<int, int> myTuple2;
Tuple<string, int, decimal> myTouple2;
Tuple<string, int, decimal, char> myTouple3;
Tuple<string, int, decimal, char, int> myTouple4;
Tuple<string, int, decimal, char, int, string> myTouple5;
Tuple<string, int, decimal, char, int, string, dynamic> myTouple6;
Tuple<string, int, decimal, char, int, string, dynamic, int> myTouple7;

You can pass any type to it including object of any class.

Declare Tuple without using Tuple class –

You heard it right. You can declare a Tuple directly without using any class.

(string, int) employeeTuple;

Super easy. Here you just need to use the parentheses and write your type directly without using any extra keyword or class.

And the best part is – There is no limitation on the number of types. You can pass even more than 8 types using this approach.

Let’s create a tuple with more than 8 types –

(string, int, decimal, char, int, string, dynamic, int, int, int, int) myTuple;

How to assign values to Tuples –

Let’s assign some values to the tuple created using Tuple class.

Since we need to assign value to a tuple that was created using Tuple class so must have the compatible type. And this type can be created using Tuple.Create method.

Tuple<string, int> employeeTuple;
employeeTuple = Tuple.Create("Nitish", 1);

You can also declare and assign the value in the same line –

Tuple<string, int> employeeTuple = Tuple.Create("Nitish", 1);

Now, Let’s assign the value to our second type –

(string, int) employeeTuple;
employeeTuple = ("Nitish", 1);

or

(string, int) employeeTuple = ("Nitish", 1);

Note: You can increase or decrease the number of types as per your project need.

Part – 2 How to use Tuple elements in C#