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

senior

When would you use delegates in C#?

middle

What is an anonymous function in C#?

junior

What is the difference between a class and a structure ?

Bình luận

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

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