Simple Tuple Example

Rajesh_Kumar
Posted by Rajesh_Kumar under C# category on | Points: 40 | Views : 1238
A Tuple has many items.Each item can have any type.Basically a Tuple is an ordered sequence,immutable,fixed-size and of heterogeneous objects,i.e., each object being of a specific type.It's an static class.It's introduced in C#-4.0.
We access the tuple properties by items.
We can understand it with an example:-

Tuple<int,string,bool> tuple_name = new Tuple<int,string,bool>(5,"Rajesh",true);

// Now Access tuple properties.
if (tuple_name.Item1 == 5)
{
Response.Write(tuple_name.Item1);
}
if (tuple_name.Item2 == "Rajesh")
{
Response.Write(tuple_name.Item2);
}
if (tuple_name.Item3)
{
Response.Write(tuple_name.Item3);
}
Output:
5
Rajesh
True

//Another tuple example
public Tuple<int,int> GetAdditionAndSubtraction(int a, int b) 
{
Tuple.Create(a+b,a-b);
}

public void Get_Result()
{
var tuple_number = GetAdditionAndSubtraction(5,5);
Response.Write(tuple_number.item1);
Response.Write(tuple_number.item2);
}

Comments or Responses

Login to post response