English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In C#, anonymous types are types (classes) without any name, and the name can only contain public read-only properties. It cannot contain other members, such as fields, methods, events, etc.
Anonymous types are created using the new operator with the object initializer syntax. The implicit type variable var is used to save the reference to the anonymous type.
The following example demonstrates how to create an anonymous type variable student that contains three properties named Id, FirstName, and LastName.
var student = new { Id = 1, FirstName = "James", LastName = "Bond" };
The properties of an anonymous type are read-only and cannot be initialized with null, anonymous functions, or pointer types. They can be accessed using the dot (.) notation, as with object properties. However, you cannot change the value of the properties because they are read-only.
var student = new { Id = 1, FirstName = "James", LastName = "Bond" }; Console.WriteLine(student.Id); //Output1 Console.WriteLine(student.FirstName); //Output: James Console.WriteLine(student.LastName); //Output: Bond student.Id = 2;//Error: Value cannot be changed student.FirstName = "Steve";//Error: Value cannot be changed
The properties of an anonymous type can include another anonymous type.
var student = new { Id = 1, FirstName = "James", LastName = "Bond", Address = new { Id = 1, City = "London", Country = "UK"} };
You can also create an anonymous type array.
var students = new[] { new { Id = 1, FirstName = "James", LastName = "Bond" }, new { Id = 2, FirstName = "Steve", LastName = "Jobs" }, new { Id = 3FirstName = "Bill", LastName = "Gates" };
Anonymous types are always local to the method that defines them. They cannot be returned from a method. However, anonymous types can be passed as object type parameters to a method, but it is not recommended to do so. If you need to pass it to another method, please use struct or class instead of an anonymous type.
Generally, anonymous types are created using the Select clause of LINQ queries to return a subset of properties from each object in the collection.
class Program { static void Main(string[] args) { IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", age = 18 }, new Student() { StudentID = 2, StudentName = "Steve", age = 21 }, new Student() { StudentID = 3, StudentName = "Bill", age = 18 }, new Student() { StudentID = 4, StudentName = "Ram" , age = 20 }, new Student() { StudentID = 5, StudentName = "Ron" , age = 21 } }; var students = from s in studentList select new { Id = s.StudentID, Name = s.StudentName }; foreach(var stud in students) Console.WriteLine(stud.Id + "-" + stud.Name); } } public class Student { public int StudentID { get; set; } public string StudentName { get; set; } public int age { get; set; } }
1-John 2-Steve 3-Bill 4-Ram 5-Ron
In the above example, the select clause of the LINQ query only selects the StudentID and StudentName properties and renames them to Id and Name respectively. Therefore, it is very useful in saving memory and unnecessary code. The query result collection only contains the StudentID and StudentName properties, as shown in the following debug view.
Visual Studio supports IntelliSense for anonymous types as shown below.
Internally, all anonymous types are derived directly from the System.Object class. The compiler generates a class with some automatically generated names and applies the appropriate type to each property based on the value expression. Although your code cannot access it. You can view the name using the GetType() method.
static void Main(string[] args) { var student = new { Id = 1, FirstName = "James", LastName = "Bond" }; Console.WriteLine(student.GetType().ToString()); }