Introduction:
The Anonymous type, as the name suggests, is actually objects that have an unknown type. In other words, whose type is not known to us. Only the compiler can get its type during the compilation of the code.
In C# as we all know there is a var keyword in reference to implicitly typed variables.
This var when used with the new keyword creates an Anonymous types object, whose type is only known to the compiler during the compilation of the code.
Description
An Anonymous Type is actually a nameless class that inherits from objects.
Simply, we can say that an anonymous type is a reference to a nameless class as created. In other words, we can use it as we use a simple reference of a class and that class doesn't actually exist.
You will get a better understanding from a simple example.
Suppose in your program you needed an object having a person's First Name, Middle Name and Last Name. For this what we basically do is to create a class Person having the fields or properties FirstName, MiddleNAme and LastName. However for this simple program you don't need to create a separate class for this, you can do this using this Anonymous types. What we need to do is to create a simple Anonymous type having the person's FIrstName, MiddleName and LastName as in the following:
The Anonymous type, as the name suggests, is actually objects that have an unknown type. In other words, whose type is not known to us. Only the compiler can get its type during the compilation of the code.
In C# as we all know there is a var keyword in reference to implicitly typed variables.
This var when used with the new keyword creates an Anonymous types object, whose type is only known to the compiler during the compilation of the code.
Description
An Anonymous Type is actually a nameless class that inherits from objects.
Simply, we can say that an anonymous type is a reference to a nameless class as created. In other words, we can use it as we use a simple reference of a class and that class doesn't actually exist.
You will get a better understanding from a simple example.
Suppose in your program you needed an object having a person's First Name, Middle Name and Last Name. For this what we basically do is to create a class Person having the fields or properties FirstName, MiddleNAme and LastName. However for this simple program you don't need to create a separate class for this, you can do this using this Anonymous types. What we need to do is to create a simple Anonymous type having the person's FIrstName, MiddleName and LastName as in the following:
- var batsman = new { FirstName=”Sachin”, MiddleName=”Ramesh”, LastName=”Tendulkar”};
- var bowler = new { FirstName=”Ravindra”, MiddleName=”Chandran”, LastName=”Ashwini”};
We can also put values inside the properties (FirstName, MiddleName and LastName) from the other class.
So that's all for the theory portion. Now its time for us to get our hand dirty with writing some code, so that we better grasp the concept of Anonymous Types.
Step 1
Create a new Console C# Project name it as per your convenience (I named it “AnonymousTypes”).
Step 2
Add a class to the project (name “Cricketer.cs”) and add three fields in it of type string (FirstName, MiddleName and LastName).
Add the following Line of code to "Cricketer.cs":
- class Cricketer
- {
- public string FirstName = "Ravindra";
- public string MiddleName = "Singh";
- public string LastName = "Jadeja";
- }
In the Program.cs file add the following line of code:
- class Program
- {
- static void Main(string[] args)
- {
- //Creating an Anonymous type batsman having properies FirstName
- //MiddleName and LastName
- var batsman = new { FirstName = "Sachin", MiddleName = "Ramesh", LastName = "Tendulakar" };
- //Similarly Creating an Anonymous Types for bowler having properties
- //First Name, Middle Name and Last Name
- var bowler = new { FirstName = "Ravindra", MiddleName = "Chandran", LastName = "Ashwini" };
- //Printing Name of Batsman
- Console.WriteLine("Batsman");
- Console.WriteLine("First Name: {0}\nMiddle Name: {1}\nLast Name: {2}", batsman.FirstName, batsman.MiddleName, batsman.LastName);
- //Printing Name of Bowler
- Console.WriteLine("\nBowler");
- Console.WriteLine("First Name: {0}\nMiddle Name: {1}\nLast Name: {2}", bowler.FirstName, bowler.MiddleName, bowler.LastName);
- //Using Class(Cricketer.cs) Properties Inside the Anonymous Type object
- //Creating an Instance of the Cricketer class
- Cricketer cricketer = new Cricketer();
- var allrounder = new { FirstName = cricketer.FirstName, MiddleName = cricketer.MiddleName, LastName = cricketer.LastName };
- //Printing Name of AllRounder
- Console.WriteLine("\nAll Rounder");
- Console.WriteLine("First Name: {0}\nMiddle Name: {1}\nLast Name: {2}", allrounder.FirstName, allrounder.MiddleName, allrounder.La stName);
- Console.ReadKey();
- }
- }
I hope you have gotten the concept of “Anonymous Types”.
If you have any query or suggestions then feel free to comment.
I am including the source code.
Thanks.
No comments :
Post a Comment