C# 4.0 new features : Tuple

C# 4.0 New Feature : Tuple

Tuple is a Datastructure.

It represent specific number and sequence of elements.

Use ::

-   Return Multiple Value

 

C# string function only retrurn one value.

IF want to display multiple value then concat with string value and split this value when we use this function.

Avoid this type of problem C# 4.0 introduce Tuple.

 

Using Tuple you can easily return multiple value.

 

 

Using Tuple.Create(Item1,Item2) you can create Tuple object.

 

Using Tuple.Item1 you can get first Item of Tuple object.

 

 

Using Simple function combine multiple (id,name) value into single string

public string GetMultiValue()

        {

            // Here your LOGIC

            return "1,XYZ";

        }

 

Using Split function, split this value

string[] strarry = GetMultiValue().Split(',');

 

            string id = strarry[0].ToString();

            string name = strarry[1].ToString();

 

 

Now Using TUPLE easily getting number of value in single function,

Tuple Example

Function which return multiple Value (like id, name)

  public Tuple<string, string> GetMultiValue()

        {

            // Here your LOGIC

            return Tuple.Create("1", "XYZ");

        }

 

Use this function

 

Tuple<string, string> ts = GetMultiValue();

            string id = ts.Item1;

            string name = ts.Item2;

 

Tuple can combine upto 8 items

 

 

 

Next Recommended Reading How to use Tuple in c# 4.0