Manually destroy C# objects

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

    Say you have a class matrix and you created two matrix objects aMatrix and bMatrix. In C# you can manually destroy (finalize) an object like so:

    aMatrix = null;
    
    GC.Collect();
    

    The garbage collector will notice that your aMatrix is null and will destroy (finalize) it. Whether or not this is a good idea is a different story.

提交回复
热议问题