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

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

Câu trả lời

Destructor in C#

A destructor in C# is a special method that is automatically called when an object is about to be destroyed or reclaimed by the garbage collector. It is used to release resources, such as memory, file handles, or database connections, that were allocated by the object during its lifetime.

Destructors are declared using the following syntax:

csharp Copy
~ClassName()
{
    // Cleanup code
}

Here, ClassName is the name of the class that contains the destructor.

When to create a Destructor

You should create a destructor in the following scenarios:

  1. When your class uses unmanaged resources: If your class uses unmanaged resources, such as file handles, database connections, or native memory allocations, you should create a destructor to ensure that these resources are properly released when the object is destroyed. This helps prevent resource leaks and ensures that your application uses system resources efficiently.

  2. **When your class implements the IDisposable in...

middle

middle

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

senior

What happens when we Box or Unbox Nullable types?

middle

What is Record in C#?

entry

What is an Object ?

Bình luận

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

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