Manually destroy C# objects

前端 未结 8 2234
感动是毒
感动是毒 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

    You can't manually destroy an object "like C++ delete", all what you can do is just close any exclusive resources the object has acquired and null all references to this object, so the GC can collect it, also don't call GC.Collect() by yourself, the GC process is expensive, as it has to suspend all other threads to safely collect the objects from memory, so just trust the GC, and it will kick off when needed.

提交回复
热议问题