Manually destroy C# objects

前端 未结 8 2260
感动是毒
感动是毒 2020-11-29 06:28

I am fairly new to learning C# (from Java & C++ background) and I have a question about manual garbage disposal: is it even possible to manually destroy an object in C#?

8条回答
  •  爱一瞬间的悲伤
    2020-11-29 06:57

    If the object is not reachable then you can call GC.Collect() and the object will be destroyed. The concept of IDisposable has nothing to do with the CLR and is mostly for user code to implement to perform additional disposal logic. Calling Dispose() on an object will not free the object itself from memory, though it may very well dispose any resources that this object references.

    I should add that while what I said is a way to achieve this, in 99.9999% of applications you should never call GC.Collect() because it'll often degrade the performance of your application instead of improving it.

提交回复
热议问题