Do I need to call Dispose() on managed objects?

后端 未结 11 1845
忘了有多久
忘了有多久 2020-12-08 14:51

I can\'t believe I\'m still confused about this but, any way, lets finally nail it:

I have a class that overrides OnPaint to do some drawing. To speed things up, I c

11条回答
  •  [愿得一人]
    2020-12-08 14:57

    Others have alluded to "using" blocks for GDI objects - here's a code example:

    using( var bm = new Bitmap() )
    using( var brush = new Brush() )
    {
    
       // code that uses the GDI objects goes here
       ...
    
    } // objects are automatically disposed here, even if there's an exception
    

    Note that you can have as many "using" lines as you want for a single code block.

    I think this is a nice, clean way to deal with Disposable objects.

提交回复
热议问题