Câu hỏi phỏng vấn C#
Câu hỏi

Explain Anonymous type in C#

Câu trả lời

Anonymous Types 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#:

Syntax

To create an anonymous type, you use the new keyword followed by curly braces {} and specify the properties inside. For example:

csharp Copy
var person = new { Name = "John Doe", Age = 30 };

In this example, person is an anonymous type with two properties: Name and Age.

Inferred Property Names

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.

Immutability

Anonymous types are immutable, meaning you cannot change the values of their properties after creation.

Accessing Properties

You can access the properties of an anonymous type using dot notation, just like regular objects. For example:

csharp Copy
Console.WriteL...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What is a Destructor in C# and when shall I create one?

senior

What interface should your data structure implement to make the Where method work?

expert

What is the difference between Lambdas and Delegates?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào