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

What is the difference between a Struct and a Class in C#?

Câu trả lời

Here is a detailed answer comparing structs and classes in C#:

Structs vs Classes in C#

In C#, both structs and classes are used to define custom data types. However, there are some key differences between them:

Value Types vs Reference Types

  • Structs are value types, meaning when you create a new variable of a struct type, the variable holds the actual data of the struct.
  • Classes are reference types, meaning when you create a new variable of a class type, the variable holds a reference to an object in memory that contains the class data.

Default Constructors

  • Structs do not have a default constructor, so you must initialize all fields in the struct when it is created.
  • Classes do have a default constructor that initializes all fields to their default values if you don't provide your own constructor.

Inheritance

  • Structs cannot inherit from other structs or classes, and they cannot be inherited fr...
junior

junior

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

middle

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

middle

What is an anonymous function in C#?

expert

What is jagged array in C# and when to prefer jagged arrays over multi-dimensional arrays?

Bình luận

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

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