What happens if i don't call dispose()?

前端 未结 4 1498
别那么骄傲
别那么骄傲 2021-01-11 17:44
    public void screenShot(string path)
    {
        var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                        Scree         


        
4条回答
  •  遥遥无期
    2021-01-11 18:13

    If a type implements the IDisposable interface, you should definitely call the Dispose method (either explicitly or by a using block).

    What happens if i don't call dispose()?

    If you don't do so, the destructor (finalizer) is responsible for freeing the resources; however, it has some drawbacks:

    • Is not deterministic: finalizers are executed by the GC on a dedicated thread. The GC decides when to run them. If a reference is kept to the object (eg. in main app window), it is possible that the finalizer will not be executed until you exit the application.
    • Overhead: unless the finalizer is suppressed, the GC has some todo with the objects to destroy.
    • Dangerous: if a finalizer throws an exception, it is considered fatal and will crash the whole application.

提交回复
热议问题