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:

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:

Console.WriteL...
middle

middle

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

middle

What is Virtual Method in C#?

senior

What is the yield keyword used for in C#?

senior

What is the difference between System.ApplicationException class and System.SystemException class?

Bình luận

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

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