Explain Anonymous type in C#
Explain Anonymous type in C#
Anonymous types in C# are a way to create simple, lightweight objects without explicitly defining a class. They allow you to create objects on the fly, encapsulating a set of properties, without the need to define a separate class or struct.
Here are the key points about anonymous types in C#:
To create an anonymous type, you use the new
keyword followed by curly braces {}
and specify the properties inside. For example:
var person = new { Name = "John Doe", Age = 30 };
In this example, person
is an anonymous type with two properties: Name
and Age
.
The property names of an anonymous type are inferred from the left-hand side of the assignment. In the previous example, Name
and Age
are the property names.
Anonymous types are immutable, meaning you cannot change the values of their properties after creation.
You can access the properties of an anonymous type using dot notation, just like regular objects. For example:
Console.WriteL...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào