.NET interview questions: - What are Tuples and when to use Tuples?

 Posted by ArticlesMaint on 8/16/2011 | Category: .NET Framework Interview questions | Views: 5683

Tuple: - A tuple is a data structure that has a specific number and sequence
of elements. An example of a tuple is a data structure with three elements
(known as a 3-tuple or triple) that are used to store an identifier such as a
person's name in the first element, a year in the second element, and the
person's income for that year in the third element. The .NET Framework directly
supports tuples with one to seven elements. In addition, you can create tuples
of eight or more elements by nesting tuple objects in the Rest property of a
Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object.

In simple words Tuple are nothing but they are objects which helps us to
logically group other kind of objects inside.

Let’s first see in what kind of scenario Tuple are applicable.

Suppose we have a simple string which has Customer’s, FirstName, MiddleName,
LastName and PhoneNumber like below code snippet.

string str = "Feroz S Shaikh 966457"; // Here you can see that the Customer's 

Name and Phone Number are Seperated by (‘ ’)spaces.

Now, you would like to parse the data and take the data in to individual
variables like below code snippet.

string strFirstName = "";//this variable will hold Customer FirstName.

string strMiddleName = "";//this variable will hold Customer MiddleName.
string strLastName = "";//this variable will hold Customer LastName.
double PhoneNumber = 0;//this variable will hold Customer PhoneNumber.

In order to parse data into individual variables, we have to create a function
with “Out” parameters and have to use “Split” function like below code snippet.

static void ParseData(string strData, out string strFirstName, out string strMiddleName, out string strLastName, out double PhoneNumber)

string[] ArrayData = new string[3];//Created Array with Size 3.
ArrayData = strData.Split(' ');//Used Split function to split the data.

strFirstName = ArrayData[0];//Passed the data to strFirstName.
strMiddleName = ArrayData[1];//Passed the data to strMiddleName.
strLastName = ArrayData[2];//Passed the data to strLastName.
PhoneNumber = Convert.ToDouble(ArrayData[3]);//Passed the data to PhoneNumber.

Still now we have done with parsing the data.

Now just invoke this function from the main class like below code snippet.

ParseData(str, out strFirstName, out strMiddleName, out strLastName, out PhoneNumber);

Now, let just display the result for that just add the below code snippet.

Console.WriteLine("FirstName :" + strFirstName);

Console.WriteLine("MiddleName : " + strMiddleName);
Console.WriteLine("LastName : " + strLastName);
Console.WriteLine("PhoneNumber : " + PhoneNumber.ToString());

Let’s see how the result set look like.

Now, the above code is nice its work’s properly but the concern here is the code
tidiness, in current scenario all the variables are individual variables in case
if you want to parse the data around it would be very tedious job to do and it
would make your very lengthy and not be very easy to read.

The solution for the above code is that if you can club the individual variables
all together in to an object and use that object to pass the data anywhere you
want to that would bring down our code to a great extent.

So Tuples help us out to achieve the above and make our code more readable and

Let’s see how exactly help us to achieve the above, to do that follow the
following steps.

Step1: - create a new project Console Application for that Go To > New > File
> Project > Windows > Select Console Application.

Step2: - create a string variable containing data like below code snippet.

string str = "Feroz S Shaikh 966457";

Step3: - creating tuple like below code snippet.

static Tuple<string, string, string, double> ParseData(string strData) //

created tuple with three string data type and one double type.

string[] ArrayData = new string[3];//Created Array with Size 3.
ArrayData = strData.Split(' ');//Used Split function to split the data.
//assigned the data to the tuple object.
return Tuple.Create<string, string, string, double>

Step4: - Now receive the tuple in the main class like below code snippet and

var CustomerInformation = ParseData(str);//recieved the tuple.

//assigned the tuple data.
Console.WriteLine("FirstName :" + CustomerInformation.Item1);
Console.WriteLine("MiddleName : " + CustomerInformation.Item2);
Console.WriteLine("LastName : " + CustomerInformation.Item3);
Console.WriteLine("PhoneNumber : " + CustomerInformation.Item4);

In the above code snippet as soon as you click CustomerInformation(.)(dot) the
VS business intelligence will show like below diagram.

Now just run your application and will see the result like below diagram.

Also view following video on why anonymous types are better than tuples.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response