Explain when to use F...
Explain when to use F...
Here is a concise answer to the interview question "Explain when to use Finalize vs Dispose in C#":
Use the Dispose
method when you need to explicitly free unmanaged resources held by an object, such as file handles, database connections, or native resources[1][2][3]. Dispose
should be called by the consumer of the object when they are done using it[1][4]. Implementing Dispose
allows you to take advantage of the using
statement to ensure Dispose
is always called[4].
Use the Finalize
(destructor) method as a last resort to free unmanaged resources if Dispose
was not called[1][2][3]. The garbage collector will call Finalize
when the object is about to be collected, but this is non-deterministic and Finalize
is slower than Dispose
[2][3][5].
It is recommended to implement both Dispose
and Finalize
together us...
expert
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào