.NET interview questions: - Prove that anonymous Types better than Tuples.
- By Shiv Prasad Koirala in ASP.NET
- Aug 18th, 2011
- 8825
- 0
.NET interview questions: - Prove that anonymous Types better than Tuples.
Anonymous Type: - Anonymous Types help us to create an object without declaring its data type and also help us to create properties with good meaningful names. Since the name of the data type is not specified that the type is referred to as an Anonymous Type.
Tuples: - In simple words Tuple are nothing but they are objects which helps us to logically group other kind of objects inside.
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
Let's first see in what kind of scenario we will use "Tuples" and "Anonymous Types".
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.
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()); Console.ReadLine();
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 and Anonymous Types help us out to achieve the above solution and make our code more readable and understandable.
Now, we will first see how exactly Tuples help in above scenario and what is the drawback of using Tuples in the above scenario and later we will see how Anonymous Types helps us to make your code better than using Tuples.
So, let see how Tuples helps to make code better in the above scenario.
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.
Step4: - Now receive the tuple in the main class like below code snippet and also display data.
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); Console.ReadLine();
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.
Now, let see what the negative point of using Tuples is.
In above diagram you can see that there is no way to identify that Item1 represent the FirstName, Item2 represent the MiddleName, Item3 represent the LastName and Item4 represent the PhoneNumber. So clearly in terms of writing a maintainable code this is not desirable.
If somehow we can change this Item1, Item2, Item3 and Item4 in to good meaningful properties name like FirstName, MiddleName, LastName and PhoneNumber than it would be much easy to understand the code in better manner. In this kind of scenario Anonymous Types help us out.
Let's see how exactly Anonymous Type help to solve the above problem for that you just need to 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: - create an Anonymous Type like below code snippet.
In the above code snippet you can see that we have created an Anonymous Type with FirstName, MiddleName, LastName, PhoneNumber and this is getting type casted to object because we cannot return Anonymous Type object outside the function straight forward. We need to type caste it like below code snippet.
Step4: - Now receive Anonymous Type in the main class like below code snippet and also display data.
var CustomerInformation = Cast(ParseData(str),new FirstName="",MiddleName="", LastName="",PhoneNumber=0}); //assigned the data. Console.WriteLine("FirstName :" + CustomerInformation.FirstName); Console.WriteLine("MiddleName : " + CustomerInformation.MiddleName); Console.WriteLine("LastName : " + CustomerInformation.LastName); Console.WriteLine("PhoneNumber : " + CustomerInformation.PhoneNumber); Console.ReadLine();
In the above code snippet as soon as you type CustomerInformation(.)(dot) the VS business intelligence will show properties like below diagram.
Now, in the above diagram you can clearly see that Item1, Item2, Item3 and Item4 is now being completely replaced by good meaningful property names like FirstName, MiddleName, LastName and PhoneNumber which become much easier to read and understand the code.
Now just run your application and will see the result like below diagram.
Following view the video on "Why anonymous types are better than tuples?"
Also see our tutorials on Dotnet interview questions and answers
Regards,
View more author's blog on Most asked Dotnet interview questions
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more