Hi friends! Today we are going to learn about Anonymous Types in C#.
Let's start with the Introduction
Example:
Example:
Example:
Important Points:
An Anonymous method can be used as event handlers.
Without Anonymous Method:
we can see that without anonymous method we need all 3 steps.
With Anonymous Method:
we can see that with an anonymous method we do not need to create a separate method for delegate references.
Anonymous method limitations:
Happy Coding 😉
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 😉