Let's start with the Introduction
Anonymous is a type that doesn't have any name. Anonymous type allows you to create a new type by using the new keyword and object initializer syntax without defining its class. The var is used to hold the reference of anonymous types. The Type of anonymous is generated by the c# compiler that is accessible within the block. All Properties of the anonymous type is read-only.
Example:
var anonymous = new { ID = 1, Name = "Same", Age = 26 }; anonymous.ID = 27; //this will give a complie time error because all properties of anonymous type are read-onlyDescription:
Here we can see that we have created a new type with new keyword and object initializer syntax without defining a class and reference of this type hold by var. And we set values of anonymous type properties.
We cannot pass an anonymous object as a parameter to a method because of the scope of its presence within the method or lock. But you can only pass an anonymous object as a parameter to a method that type is dynamic. But it is not recommended.
Example:
var anonymous = new { ID = 1, Name = "Same", Age = 26 }; MyData.GetData(anonymous); //calling method class MyData { public static void GetData(dynamic d) { Console.WriteLine(d.Name); } }Real Use Of Anonymous Type:
The real use of Anonymous Type is in LINQ query select clause where we can define new properties which are not defined in any class.
Example:
List data = new List { new MyData { FirstName = "Jignesh", LastName = "Trivedi", MiddleName = "G", DOB =new DateTime(1990, 12, 30) }, new MyData { FirstName = "Tejas", LastName = "Trivedi", MiddleName = "G", DOB = newDateTime(1995, 11, 6) }, new MyData { FirstName = "Rakesh", LastName = "Trivedi", MiddleName = "G", DOB = newDateTime(1993, 10, 8) }, new MyData { FirstName = "Amit", LastName = "Vyas", MiddleName = "P", DOB = newDateTime(1983, 6, 15) }, new MyData { FirstName = "Yash", LastName = "Pandiya", MiddleName = "K", DOB = newDateTime(1988, 7, 20) } }; var s = (from p in data select new { Name = p.FirstName + " " + p.MiddleName + " " + p.LastName, DateOfBirth = p.DOB }).ToList(); class MyData { public string FirstName { get; set; } public string LastName { get; set; } public DateTime DOB { get; set; } public string MiddleName { get; set; } }Description:
Here we have to get the list of data and select the Name and DataOfBirth properties which are not defined any class this is an anonymous type property.
Important Points:
- Anonymous type object can only be passed as a parameter to a method which has a dynamic data type.
- Anonymous type can be defined using the new keyword or object initializer syntax.
- var, is used to hold anonymous type references.
- Anonymous type is a reference type and all the properties are read-only.
- The scope of an anonymous type is local to the method where it is defined.
Anonymous method in C#
- An anonymous method is a method without a name that allows us to create delegate instances without writing a separate method.
- We can pass an only input parameter to the anonymous method.
- With anonymous method delegate parameter is optional.
- Anonymous method must be assigned to a delegate
- An anonymous method can access outer variables or functions.
- An anonymous method can be passed as a parameter that accepts delegate as a parameter.
public delegate void Print(int value); class Test { public static void PrintHelperMethod(Print printDel, int val) { val += 10; printDel(val); } static void Main(string[] args) { PrintHelperMethod(delegate (int val) { Console.WriteLine("Anonymous method: {0}", val); }, 100); } }
saveButton.Click += delegate(Object o, EventArgs e) { System.Windows.Forms.MessageBox.Show("Save Successfully!"); };
public static class Program { //Step=1 declare delegates public delegate int delCustomer(int ID); static void Main(string[] args) { //Step-3 instantiated delegates delCustomer del = new delCustomer(getCustomerName); var result= del.Invoke(1); Console.WriteLine("Result"+ result); } //Step-2 create method that pass to delegate public static int IsExists(int ID) { return ID==1?1:0; } }Description:
we can see that without anonymous method we need all 3 steps.
With Anonymous Method:
public static class Program { //Step=1 declare delegates public delegate int delCustomer(int ID); static void Main(string[] args) { //Step-3 instantiated delegates delCustomer del = delegate (int ID) { return ID == 1 ? 1 : 0; }; } }Description:
we can see that with an anonymous method we do not need to create a separate method for delegate references.
Anonymous method limitations:
- It cannot contain jump statement like goto, break or continue.
- It cannot access ref or out parameter of an outer method.
- It cannot have or access unsafe code.
- It cannot be used on the left side of the is operator.
So, Guys, this is all about Anonymous Types in C#
I Hope in this post covered all the points about Anonymous Types in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
cbse 10th & 12th results 2018
ReplyDeleteKerala board 10 results 2018
Yoga Pose & health Benefits
ReplyDeleteyoga for meditation
yoga for weight loss
yoga for lower back pain
yoga for beginners
hatha yoga poses
bikram yoga poses types
yoga for inner peace
ReplyDeleteVoter Id Card
online check voter id status by name
online apply Voter Id Card Registration
Voter Id Correction
online duplicate Voter Id Card
online check process voter id 2018-19
apply for online voter card
check voter id status by date of birth
Maharashtra Voter ID Card Registration
ReplyDeleteThis is a wonderful article, Given so much info in it, You can see Travel Nearby best tourist place Best Honeymoon Destination in this page.Places to visit in Goa
Weekend Gateways Near Pune
Greetings! Very useful advice in this particular
ReplyDeletepost! It’s the little changes that make the largest changes.
Modicaren busniness plan how to earn big money in modicare
Modicare marketing business
Top-5-network-marketing-companies-in-india
Thanks have been given the opportunity to comment. Hopefully what you provided is useful for all those who need them. Visit my website if you want to know more about.Latest Breaking News in India
ReplyDeleteTop Stories News Headlines
ReplyDeleteTravel Nearby Places to visit in Uttarakhand
Best Tourist Places to visit in Haridwar
Tourist Places to Visit in Nainital
Beautifull Hill Station Auli for Couples
Tourist Place Near Bhimtal
Most Beautiful Places To Visit in Ranikhet
Tourist Places to Visit in Dehradun
Places to visit in Dhanaulti
Places to visit in Mussoorie
Thanks a bunch for sharing this with all of us you actually know what you are talking about! Bookmarked. Please also visit my site
ReplyDeleteMost famous tourist Places to visit in Rajasthan
Best Tourist Places to visit in Jaipur
Stunning Tourist Places to Visit in Jaisalmer
Beautifull Hill Station Udaipur for Couples
Incredible Places To Visit in Mount Abu
Alluring Tourist Places To Visit in Jodhpur
Most Tourist Places to Visit in Pushkar
Beautiful Places To Visit in Guwahati
ReplyDeleteStunning Tourist Places To Visit in Barpeta
Amazing Places To Visit in Goalpara
Most Beautiful Places To Visit in Dispur
Most Beautiful Places To Visit in Digboi
Stunning Tourist Places To Visit in Jorhat
Most Amazing Places To Visit in Karimganj
Beautiful Places To Visit in Nalbari
Incredible Tourist Places To Visit in Tinsukia
Popular Tourist Places To Visit in Tezpur
ReplyDeleteBeautiful Tourist Places To Visit in Meghalaya
Amazing Places To Visit in Tura
Best Tourist Places To Visit in Jowai
Most Beautiful Places To Visit in Cherrapunji
Incredible Tourist Places To Visit in Nongpoh
Most Famous Places To Visit in Shillong
Beautiful Places To Visit in Nongstoin
ReplyDeleteThanks a bunch for sharing this with all of us you actually know what I’m excited to find this page. I wanted to thank you for ones time due to this wonderful read!!
Top 11 Most Beautiful Places To Visit in Arunachal Pradesh
Most Beautiful Places To Visit in Bomdila & Things To Do
Top 6 Alluring Tourist Places To Visit in Changlang & Things To Do
Top 5 Stunning Tourist Places To Visit in Dirang & Things To Do
Top 7 Best Tourist Places To Visit in Itanagar & Things To Do
Top 7 Incredible Tourist Places To Visit in Roing
Top 8 Best Tourist Places To Visit in Tawang
Most Famous Tourist Places To Visit in Ziro & Things To Do
Beautiful Tourist Places To Visit in Pasighat
UP 12th Board Result click here
ReplyDelete