Working with .NET Framework 4.0 Tuples

Tuple is an inbuilt class type
introduced in .net
framework
4.0. This type is very useful if the developer knows when and
where to use it. A tuple in .net framework 4.0 will represent a data structure
whose element can be strongly typed. This type is directly added to the System
namespace.

Tuple Insight

Tuple is a static class and can
be injected with a list of items that are of different types. A tuple object
can be created using its constructor or by using the method factory method
called Create. Below are the code samples.

//Using the Generic constructor
var t = new Tuple<int, int, string, DateTime>(10, 25, "This is a tuple demo", DateTime.Now);
 
//Using the factory method Create
var t1 = Tuple.Create<int, int, string, DateTime>(10, 25, "This is a tuple demo", DateTime.Now);
 

Note that the Generic tuple class
is not a static class so creating the instance is possible. There are 8 such
generic tuple classes available to be used and the static Tuple class allows
the creation of all the 8 generic tuple objects by exposing the factory method
for each generic class.

At any point of time a tuple can
be injected with 1 to 7 data items and if the developer wants to add few more
then the way is to add a new tuple object itself at the 8th place.
Below is the code demonstrating it.

//Nesting tuples
var t = new Tuple<int, int>(10, 25);
var t1 = new Tuple<string, int, int, int, int, int, int, Tuple<int, int>>("This is a tuple demo", 1, 2, 3, 4, 5, 6, t);
 

As mentioned in the above code
you can keep nesting the tuple class one after the other.

Accessing the Data from a Tuple Object

The data values provided to the
tuple objects will be exposed as Item1, Item2, etc. Below is the code
demonstrating how to access the data from the tuple object.

    class Program
    {
        static void Main(string[] args)
        {
            var t = GetEmployeeTupleObject();
            Console.WriteLine("Name: {0} {1}", t.Item1, t.Item2);
            Console.WriteLine("Age: {0}", t.Item3);
            Console.WriteLine("Job Title: {0}", t.Item4);
        }
 
        private static Tuple<string, string, int, string> GetEmployeeTupleObject()
        {
            return new Tuple<string, string, int, string>("John", "Britto", 32, "Senior Developer");
        }
    }

Advantage of Using a Tuple in .NET Program

In this section let us look at
the advantages of using a tuple object in .net

1. For
a method to return multiple values prior to .net framework 4.0 is to declare
the out parameters and fetch the data through it. But with the use of tuple the
developer can send back multiple data items without using the out parameter.

2. Multiple
parameters can be clubbed into a single tuple and passed as a single parameter
to the method. Later in the method the values can be fetched by using the
tuple’s item properties.

3. Tuple
data are strongly typed so that there is no boxing or un-boxing required. This
improves the performance of the .net program.

4. If
you want to pass an employee detail from a function as shown in the above
section’s sample code in the earlier .net framework versions you may have to
create an Employee class or a struct and then populate the data. But by using the
tuple object you have achieved it in a strongly typed manner with ease and with
reduced lines of code.

5. You
can increase the storage capacity of the tuple object by creating nested Tuples.
The size is not a big constraint as the Tuple is a reference type but with a
struct this is a real problem as the struct is a value type and will be stored
on the stack.

6. As
I said Tuple is a reference type you can perform compare operations like other
reference types. Equals would compare the data and == will compare the object
address.

    class Program
    {
        static void Main(string[] args)
        {
            var t = GetEmployeeTupleObject();
            var t1 = GetEmployeeTupleObject();
 
            //Compares the data value, TRUE
            Console.WriteLine(t.Equals(t1));
            //Compares the address of the object, FALSE
            Console.WriteLine(t == t1);
        }
 
        private static Tuple<string, string, int, string> GetEmployeeTupleObject()
        {
            return new Tuple<string, string, int, string>("John", "Britto", 32, "Senior Developer");
        }
    }

Some Limitations

In this section we will look at
some limitations. As the tuple class in .net framework has lots of advantages
it also has some limitations while using them in some scenarios.

1. If
you compare a Tuple object with a custom object Employee the tuple does not
have the values mapped against well named properties like FirstName, LastName,
Age, JobTitle, etc.; rather it exposes them as Item1, Item2, Item3 and Item4.
So when a tuple object is passed to an external class then the calling class
should know in what sequence the values are being passed.

2. Since
the Tuple class does not implement an IEnumerable class you cannot implicitly
use it in a ForEach loop. If you have to use ForEach still then create a custom
class inheriting Tuple and IEnumerable.

I hope this article was informative. Happy reading!

More by Author

Must Read