Tuple C# Introduction
Tuples has been introduced in .NET framework 4.0. These are basically set of generic classes for holding set of potentially different types elements in C#.
You can view other C# related articles here.
Creating A Tuple
We can create tuple in C# in many ways as shown in the following code snippet
var tupleOne = new Tuple(1); var tupleTwo = new Tuple (1, "test"); var sevenelementTuple = new Tuple (1,2,3,4,5,6,7); var morethenSevenElement = new Tuple >(1, 2, 3, 4, 5, 6, 7, new Tuple ("Hi"));
As we can see from the above code I have created four type of tuples. Each has read only properties named Item1, Item2, Item3 and so on.
The first tuple is created with single int type element specified using the type parameter as int and giving the value of the parameter as 1 in the constructor.
tupleTwo is created with two type of type parameters i.e. int and string. Like this we can create a tuple of upto seven parameters.
If we want a tuple to have more then seven parameters we have to create a tuple with tuple as shown above. moreThenSevenElement is the tuple which is created with a tuple contained inside itself.
Other way to create a tuple would be to use the Static Method Create as shown below.
var tuple = Tuple.Create(1); var tuple1 = Tuple.Create(1, "string", DateTime.Now);
Other features would be same for this method as creation using constructor. In case of the Create method if we specify the var keyword there is no need to specify the type parameter as is the case for tuple1 in the above example as intrinsic type inference comes to rescue and type for the parameters is decided at compile time.
Accessing the Values of Tuples
We can access the values of tuples as shown in the below code.
int item = tupleTwo.Item1; string item2 = tupleTwo.Item2;
if we want to access the items of the tupleTwo from the first code snippet we can get them as shown in the above code and as shown in the below figure the items are present with their types.
We cannot assign the value to the item of a tuple after the creation of tuple. We will get a run time error as the items are readonly as shown in the below figure.
Comparing Tuples
public static void ComparingTuple() { var tuple1 = new Tuple(1, "Test"); var tuple2 = new Tuple (1, "Test"); var equality = tuple1 == tuple2; // false var equality1 = tuple1.Equals(tuple2); //true }
As shown in the above code snippet we can compare tuple in two ways i.e. reference comparison and value comparison.
If we compare the two tuples using (==) operator, the tuples are compared reference wise and it returns false and if we compare using Equals() method the values of the two tuples are compared and we get true in above code.
Utilization in C#
After reading about tuples the question arises why do want to use tuples in C#.One use of tuple is to return multiple values from a method as shown below.
private TupleGetUserIDAndPwd() { return new Tuple ("UserID", "password"); } private void AnotherMethod() { var keys = GetUserIDAndPwd(); }
As shown in the above code we can use tuple to return multiple value as we are returning userID and pwd otherwise we had to create a class for this task or return array of object which would be strongly typed.
The other use would be to have single set of data from database where a tuple is data record and items are individual columns of the record.
Leave a Reply