Introduction
In this article i will explain about difference between Tuples and Anonymous Type with console Examples.
Description
First we understand what is Tuple and Anonymous and why we used them .
Anonymous :
Anonymous types are the new concept in C#3.0 which allow to create new types without defining them. Anonymous types have been introduced to support one of the most useful features called LINQ. It's most useful when you are querying collection of object using LINQ query and you want to return only a few of its properties. Anonymous type is class type which is directly derived from “System.Object” and it cannot be cast any type other than the object.All properties of anonymous
type are read only
A var data type and new keyword is used to create an anonymous type.
var emp = new { Name = "JP", Address = "New Delhi", Phone= 7838069};
Example:
public static object ParseString(string input)
{
string[] strArr = new string[3];
strArr = input.Split(' ');
return new { FirstName = strArr[0], LastName = strArr[1], Certification = strArr[2], Phone = Convert.ToInt64(strArr[3]) };
}
static void Main(string[] args)
{
String str = "Jatin Phulera MCT 7838069550";
var AnonymousData= Cast(ParseString(str), new { FirstName = "", LastName = "", Certification = "", Phone = 0 });
Console.WriteLine("First Name : " + AnonymousData.FirstName);
Console.WriteLine("Last Name : " + AnonymousData.LastName);
Console.WriteLine("Certification : "+ AnonymousData.Certification);
Console.WriteLine("Phone: " + AnonymousData.Phone.ToString());
Console.Read();
}
static T Cast<T>(object obj, T type)
{
return (T)obj;
}
For Anonymous type, Visual studio able to show intellisense because type is known at the time of compilation. And Anonymous Type shows Strongly Type properties you can see in above image, all the four properties which i define using Anonymous in method are displayed in intellisense at compilation time ,which is the drawback of Tuples.
Tuples :
According to MSDN "A tuple is a data structure that has a specific number and sequence of elements".
Tuples can be very handy for developers, allowing them to return multiple values from a function,
The tuples are not new in programming. They are already used in F#, Python and databases. However, they are new to C#. The tuples were introduced in C# 4.0 with dynamic programming.
Example: -
public static Tuple<string,string ,string ,long> ParseString(string input)
{
string[] strArr = new string[3];
strArr = input.Split('
');
return Tuple.Create<string, string, string, long>(strArr[0], strArr[1], strArr[2], Convert.ToInt64(strArr[3]));
}
//In C#,
Tuple
is a static
class
that implements the "Factory" Pattern to create instances of Tuples.
We can create an instance of a Tuple using the constructor or the static
method "Create
".
static void Main(string[] args)
{
String str = "Jatin
Phulera MCT 7838069550";
var TupleData= ParseString(str);
Console.WriteLine("First Name :
" + TupleData.Item1);
Console.WriteLine("Last Name :
" + TupleData.Item2);
Console.WriteLine("Certification
: " + TupleData.Item3);
Console.WriteLine("Phone:
" + TupleData.Item4);
Console.Read();
}
Why should we use them
Tuples provide a quick way to group multiple values into a single result, which can be very useful when used as a return of function, without the need to create parameters "
ref
" and /
or "out
".
DrawBack
The DrawBack with Tuples is Tuples do not have names that may have some significance. The attributes of a tuple are called "
Item1
", "Item2
", and so on. You can not find your actual Property name while printing the values. You can see this in below image.Hope you can enjoy this article and Happy coding.
0 comments:
Post a Comment