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

What is the use of the IDisposable interface?

Câu trả lời

The IDisposable interface in C# is used to provide a standard way to release unmanaged resources that are held by an object. When an object holds unmanaged resources, such as file handles, database connections, or network sockets, it is important to ensure that these resources are properly released when they are no longer needed. This is because unmanaged resources are not automatically managed by the .NET garbage collector, and if they are not released, they can lead to resource leaks, which can cause performance issues or even crashes in the application.

The IDisposable interface defines a single method, Dispose(), which is used to release the unmanaged resources held by an object. When an object implements the IDisposable interface, it should release all of its unmanaged resources in the Dispose() method.

Here are some common scenarios where the IDisposable interface is used:

  1. File I/O: When working with files, it is important to ensure that file handles are properly closed when they are no longer needed. This can be done by implementing the IDisposable interface in the class that encapsulates the file operations.

  2. Database connections: When working with databases, it is important to ensure that database connections are properly closed when they are no longer needed. This can be done by implementing the IDisposable interface in the class that encapsulates the database operations.

  3. Network sockets: When working with network sockets, it is important to e...

middle

middle

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

middle

What is Extension Method in C# and how to use them?

senior

When to use ArrayList over array[] 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