pavanarya – Pavan Kumar Aryasomayajulu <br> My New Blog http://xyzcoder.com

Contact Email : pavan.aryasomayajulu@gmail.com

Tuple Class

leave a comment »

“A tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a positive integer”

So coming to .Net a Tuple is a data structure that was introduced in Framework 4.0 and contains specific number and sequence of elements.

A Tuple can have a maximum of 8 elements (directly) like below


 Tuple tuple1 = new Tuple(1);
Tuple tuple1 = new Tuple(1,"2",3);
Tuple tuple1 = new Tuple(1,2,3,"4");
//In the same way we can have max of 8 element of different datatypes
//A Tuple can also be create using Create() method instead of using new Tuple<>()
 Tuple tuple1 = Tuple.Create(1, 2, 3);

In addition to 8 elements, a Tuple also supports n number of elements in an indirect way.

//Tuple(Of T1, T2, T3, T4, T5, T6, T7, TRest)
static void Main(string[] args)
        {

            Tuple tuple1 = Tuple.Create(1, 2, 3);

            var tuple2 = new Tuple>
                (4,tuple1);

            var tuple3 = new Tuple>
                (4, 5, 6, 7, 8, 9, 10, tuple1);

            var tuple4 = new Tuple                Tuple>>
                ("Numbers", 11, 12, 13, 14, 15, 16, tuple3);

        // Retreving tuple elements

         int tupl2fifthelement= tuple2.Item2.Item2;
         int tupl3tenthelement = tuple3.Rest.Item3;
         int tuple4lastelement=tuple4.Rest.Rest.Item3;

        }

In the above code tuple1 contains 3 elements, tuple2 contains 4 elements, tuple3 contains 10 elements and atlast tuple4 contains 17 elements.

So inorder to get the nested tuple’s elements we should use a property called Rest like tuple3.Rest.Item3;

Rest property gets the current Tuple(Of T1, T2, T3, T4, T5, T6, T7, TRest) object’s remaining components.

Uses Of Tuple

1. Suppose if we want to return multiple values from a function and we don’t want to make use ‘out’ or ‘byref’ then we can make use of this.

class Example
    {</pre>
static void Main(string[] args)
 {

Tuple<int, string, int> tup = ReturnMultiplevalues();
 Console.WriteLine(tup.Item1);
 Console.WriteLine(tup.Item2);
 }

static Tuple<int, string, int> ReturnMultiplevalues()
 {
 int i = 10;
 String val = "Pavan";
 int j = 20;

return new Tuple<int, string, int>(i, val, j);
 }
<pre>    }

2. We can also make use of a tuple to represent a dataset. What i mean is each record in a dataset can be presented in the form of a tuple and the fields of the record can be represented as a tuple elements.

Written by pavanarya

November 23, 2011 at 4:54 pm

Posted in Asp.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: