Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C# 4.0
Article

An introduction to Tuple

Rate me:
Please Sign up or sign in to vote.
2.93/5 (27 votes)
25 Oct 2010CPOL4 min read 49.5K   111   2   25
This article will explain about some of the benefits of using Tuple in C#4.0

Introduction

C#4.0 has introduce a new feature call Tuple.

Definition

In mathematics and computer science, 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. There is also one 0-tuple, an empty sequence. (From Wikipedia)

Purpose:

Some time we need to return more than one value from a method or function.

Using the code

Let us perform a simple experiment for the basic arithmetic operations for the concept to understand

Let us first write our Calculate Function

<summary />/// Calculate <param name="div" /></param />

private void Calculate(out int add, out int sub,out int mul,out int div)
{
   int num1 = 5;
   int num2 = 4;
   add = num1 + num2;
   sub = num1 - num2;
   mul = num1 * num2;
   div = num1 / num2;
}

Older approach (Till dotnet 3.5)

Approach 1: Using OUT parameter

The calling function is as under

1.jpg

The output is as expected

2.jpg

Approach 2: Using REF parameter

Similar to the first one but change OUT to REF

Approach 3: Using custom datatype

Create a CalculateEntity as depicted under

3.jpg

and modify our Calculate function as under

<summary />/// Calculate
private static CalculateEntity Calculate()
{
            CalculateEntity objCalculateEntity = new CalculateEntity();
            int num1 = 5;
            int num2 = 4;
            objCalculateEntity.Add = num1 + num2;
            objCalculateEntity.Sub = num1 - num2;
            objCalculateEntity.Mul = num1 * num2;
            objCalculateEntity.Div = num1 / num2;
            return objCalculateEntity;
}

The calling function will now be

4.jpg

 Approach 4:New Approach(Dotnet 4.0) - Using Tuple

5.jpg

As can be seen that using Tuple we can return multiple values from the function/method in a much more better and readable way.

The above example shows that tuple return s only the same type of datatypes.

Now lets see how we can return different datatypes from tuple

Let us modify the above Calculate function such that, the last argument will be double.

6.jpg

The result is as expected

7.jpg

Overloaded methods of Tuple

A Tuple has 8 overloaded methods whose signatures are given as under.

1-Tuple or Singleton

public static Tuple<t1 /> 
    Create<t1 />
    (T1 item1);

2-Tuple or Pair

public static Tuple<t1, /> 
     Create<t1, />
     (T1 item1, T2 item2);

3-Tuple or Triple

public static Tuple<t1, /> 
     Create<t1, />
     (T1 item1, T2 item2, T3 item3);

4-Tuple or Quadruple

public static Tuple<t1, /> 
     Create<t1, />
     (T1 item1, T2 item2, T3 item3, T4 item4);

5-Tuple or Quintuple

public static Tuple<t1, /> 
     Create<t1, />
     (T1 item1, T2 item2, T3 item3, T4 item4,
     T5 item5);

6-Tuple or SexTuple

public static Tuple<t1, /> 
     Create<t1, />
     (T1 item1, T2 item2, T3 item3, T4 item4, 
     T5 item5, T6 item6);

7-Tuple or SepTuple

public static Tuple<t1, />
    Create<t1, />
    (T1 item1, T2 item2, T3 item3, T4 item4, 
     T5 item5, T6 item6, T7 item7);

8-Tuple or Octuple

public static Tuple<t1, />> 
      Create<t1, />
      (T1 item1, T2 item2, T3 item3, T4 item4, 
       T5 item5, T6 item6, T7 item7, T8 item8);

A careful observation of the last overloaded method i.e. Octuple reveals that the last argument is again a tuple which indicates that it can carry further 8 items and the process goes on.

Let us see how can we use the last overloaded method.

Here I will build a simple Multiplication table that will use the last overloaded method.

The MultiplicationTable function is as under

8.jpg

We have highlighted the 8th parameter as how to use that.

The calling function is equally simple

int number = 5;
var tuple = MultiplicationTable(number);

    string format =
    "Multiplcation Table of {0} is" + Environment.NewLine +
    "------------------------------" + Environment.NewLine + Environment.NewLine +
    "{0} * 1     = {1} " + Environment.NewLine +
    "{0} * 2     = {2} " + Environment.NewLine +
    "{0} * 3     = {3} " + Environment.NewLine +
    "{0} * 4     = {4} " + Environment.NewLine +
    "{0} * 5     = {5} " + Environment.NewLine +
    "{0} * 6     = {6} " + Environment.NewLine +
    "{0} * 7     = {7} " + Environment.NewLine +
    "{0} * 8     = {8} " + Environment.NewLine +
    "{0} * 9     = {9} " + Environment.NewLine +
    "{0} * 10    = {10} " + Environment.NewLine;

    string result = string.Format(
    format
    , number
    , tuple.Item1
    , tuple.Item2
    , tuple.Item3
    , tuple.Item4
    , tuple.Item5
    , tuple.Item6
    , tuple.Item7
    , tuple.Rest.Item1
    , tuple.Rest.Item2
    , tuple.Rest.Item3

    );
Console.WriteLine(result);
Console.ReadKey(true);

The output is as under

9.jpg

As can be seen that it is very simple to use that.

But what if we need to determine at runtime the number of parameter on our tuple.

Its a bit tricky but by using reflection can be easily done. Let’s have a look

10.jpg

Though a complete details of reflection is not at all in the scope of this article but we will walk through as what the code is doing.

In the line

Type t = typeof(CSharpDemo.Program);

we are obtaining the type of the class which is Program here.

Then by the usage of the InvokeMember, we are invoking the method which is MultiplicationTable here . Since it accepts a parameter henceforth we are passing the same in the last argument.

11.jpg

The InvokeMember returns an object and after type casting that to string followed by string replacement we are splitting that by comma(,).

Then by applying the new Enumerable.Zip extension method whose generalized signature is Enumerable .Zip<TFirst,TSecond,TResult>, we are merging the two sequences in order to bring the needed display format and lastly by using the Foreach extension method we are displaying the needed result.

Create Custom Extension methods on Tuple

Tuples lacks the luster of ItemCount or ItemValues etc. But we can create so by using extension method on Tuple. Let us see how.

Create a class Call TupleExtension.cs and write the below code

using System;
using System.Linq;
using System.Reflection;

namespace CSharpDemo
{
    public static class TupleExtension
    {
        public static Tuple<int, /> GetLengthAndMemeberValues(this Tuple<int, /> targetTuple, string methodName, object[] param)
        {
            //Obtain the type of the class 
            Type t = typeof(CSharpDemo.Program);

            //Create an instance of the class
            object classInstance = Activator.CreateInstance(t);

            //Invoke the method
            object memberValues = t.InvokeMember(methodName, BindingFlags.InvokeMethod, null, classInstance, param);
            
            //Assign the needed value to the target tuple
            targetTuple = new Tuple<int, />
                 (ItemCount(memberValues), memberValues);

            return targetTuple;
        }

        /// <summary />
        /// ItemCount
        /// Get the count of the number of items
        /// </summary />
        /// <param name="o" /></param />
        /// <returns />int</returns />

        public static int ItemCount(object o)
        {
            return o.ToString()
            .Replace("(", string.Empty)
            .Replace(")", string.Empty)
            .Split(',').Count();
        }
        
    }
}

To use the extension method we can go ahead as mentioned below

Tuple<int,object /> testTuple = new Tuple<int,object /> (0,null);

var resultantTuple = testTuple
                    .GetLengthAndMemeberValues("MultiplicationTable"
                    , new object[] { number });

Console.WriteLine("Number of TupleItems" + resultantTuple.Item1);
Console.WriteLine("Items of Tuple" + resultantTuple.Item2);

The output is as under

12.jpg

Conclusion:

In this short tutorial, we tried to explain most of the ways of using tuple, its usefulness and also an extension method of how to increase the tuple capability of overcoming its drawback which may come into use. This article also gives a small overview of the new Zip extension method and its usage. I have attached the sample code along with the article. Feel free to use and modify it as per your requirement.

Comments on the topic are highly appreciated for the improvement of the topic.

Thanks for reading the article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 1 Pin
shek12410-Oct-11 4:09
shek12410-Oct-11 4:09 
GeneralMy vote of 3 Pin
Adeel Mahmood23-Nov-10 19:38
Adeel Mahmood23-Nov-10 19:38 
GeneralMy vote of 1 Pin
Batzen28-Oct-10 1:56
Batzen28-Oct-10 1:56 
GeneralMy vote of 4 Pin
Eshwer27-Oct-10 7:51
Eshwer27-Oct-10 7:51 
GeneralMy vote of 1 Pin
M8ix26-Oct-10 18:39
M8ix26-Oct-10 18:39 
GeneralRe: My vote of 1 [modified] Pin
Niladri_Biswas26-Oct-10 20:18
Niladri_Biswas26-Oct-10 20:18 
GeneralMy vote of 1 Pin
JF201526-Oct-10 18:07
JF201526-Oct-10 18:07 
GeneralRe: My vote of 1 [modified] Pin
Niladri_Biswas26-Oct-10 20:19
Niladri_Biswas26-Oct-10 20:19 
GeneralMy vote of 5 Pin
depakb26-Oct-10 17:43
depakb26-Oct-10 17:43 
GeneralMy vote of 1 Pin
Bad code hunter26-Oct-10 12:39
Bad code hunter26-Oct-10 12:39 
GeneralMy vote of 1 Pin
ashved26-Oct-10 7:40
ashved26-Oct-10 7:40 
GeneralRe: My vote of 1 Pin
Niladri_Biswas26-Oct-10 20:29
Niladri_Biswas26-Oct-10 20:29 
General[My vote of 2] not your best work Pin
BillW3326-Oct-10 6:37
professionalBillW3326-Oct-10 6:37 
GeneralRe: [My vote of 2] not your best work Pin
Niladri_Biswas26-Oct-10 20:30
Niladri_Biswas26-Oct-10 20:30 
GeneralMy vote of 1 Pin
Jay Riggs26-Oct-10 5:24
Jay Riggs26-Oct-10 5:24 
GeneralMy vote of 1 PinPopular
Sacha Barber26-Oct-10 3:24
Sacha Barber26-Oct-10 3:24 
GeneralRe: My vote of 1 Pin
Niladri_Biswas26-Oct-10 3:51
Niladri_Biswas26-Oct-10 3:51 
GeneralRe: My vote of 1 PinPopular
Sacha Barber26-Oct-10 4:29
Sacha Barber26-Oct-10 4:29 
GeneralRe: My vote of 1 Pin
Niladri_Biswas26-Oct-10 16:41
Niladri_Biswas26-Oct-10 16:41 
GeneralMy vote of 1 Pin
Seishin#26-Oct-10 2:03
Seishin#26-Oct-10 2:03 
GeneralMy vote of 1 Pin
leppie26-Oct-10 1:53
leppie26-Oct-10 1:53 
GeneralRe: My vote of 1 Pin
Niladri_Biswas26-Oct-10 20:45
Niladri_Biswas26-Oct-10 20:45 
GeneralMy vote of 2 Pin
taras_b26-Oct-10 0:31
taras_b26-Oct-10 0:31 
GeneralMy vote of 2 Pin
User 461199625-Oct-10 23:57
User 461199625-Oct-10 23:57 
GeneralRe: My vote of 2 Pin
Niladri_Biswas26-Oct-10 0:16
Niladri_Biswas26-Oct-10 0:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.