7 (seven) ways to define Tuple elements in c#
You can define the tuple elements in many ways. Here are the most effective 7 (seven) ways to define tuple elements in c#.
First: Define a tuple using the element type
Tuple elements can be defined directly by specifying the element type
–
(string, int) personTuple = ("Nitish", 1);
To access the elements, we can use Item1
& Item2
field of the personTuple
.
Console.WriteLine($"First element - {personTuple.Item1}");
Console.WriteLine($"Second element - {personTuple.Item2}");
//output
First element - Nitish
Second element - 1
Second: Define a tuple using the var keyword
We can define the tuple elements directly using the var
keyword.
var personTuple = ("Nitish", 1);
To access the elements we can use Item1
& Item2
field.
Console.WriteLine($"First element - {personTuple.Item1}");
Console.WriteLine($"Second element - {personTuple.Item2}");
//output
First element - Nitish
Second element - 1
Third: Define a tuple by changing the default positional name
We can also specify a name to the tuple elements type.
(string name, int id) personTuple = ("Nitish", 1);
Now it is more meaningful to read the elements and we can access the elements using these new positional names.
Console.WriteLine($"First element - {personTuple.name}");
Console.WriteLine($"Second element - {personTuple.id}");
//output
First element - Nitish
Second element - 1
Fourth: Define a tuple using discrete Tuple variable –
While working with tuples in c#, there is no need to define the name for the tuple. We can define the tuple directly only with the elements name and type –
(string Name, int Id) = ("Nitish", 1);
You can access the tuple elements directly with the positional name.
Console.WriteLine($"First element - {Name}");
Console.WriteLine($"Second element - {Id}");
//output
First element - Nitish
Second element - 1
Fifth: Implicitly types variables using var
We can define a tuple in c# by writing the var
keyword outside the parantheses.
var (Name, Id) = ("Nitish", 1);
Again, we can access the tuple elements directly by using the elements name.
Console.WriteLine($"First element - {Name}");
Console.WriteLine($"Second element - {Id}");
//output
First element - Nitish
Second element - 1
Sixth: Define a tuple by mixing the var keyword and element name
We can also combine the explicit type and the var keyword to define a tuple –
(string Name, var Id) = ("Nitish", 1);
We can us the tuple elements directly by using elements name.
Console.WriteLine($"First element - {Name}");
Console.WriteLine($"Second element - {Id}");
//output
First element - Nitish
Second element - 1
Seventh: Define a tuple using Tuple keyword –
A tuple can also be defined using Tuple class.
Tuple<string, int> personTuple= Tuple.Create("Nitish", 1);
Execution of tuple –
Console.WriteLine($"First element - {personTuple.Item1}");
Console.WriteLine($"Second element - {personTuple.Item2}");
//output
First element - Nitish
Second element - 1
Don’t forget to share this information with your friends.