using using

M

Martijn Mulder

When I want to invoke Dispose() automatically on an object, I can use

using(object)
{
//do something with object
}

and after the closing brace the object is Disposed(). But what get
disposed when I use code like this

override protected void OnPaint(PaintEventArgs e)
{
using(e.Graphics.Clip=GetMyCustomRegion())
{
e.Graphics.DrawImage(bitmap,0,0);
}
}

Is the Region object returned by GetMyCustomRegion() succesfully
Disposed()? How does it influence the Clip-property of the Graphics object?

Also, can I nest 'using' statement like this:

override protected void OnPaint(PaintEventArgs e)
{
using(e.Graphics.Clip=GetMyCustomRegion())
using(Bitmap bitmap=new Bitmap(34,55))
{
e.Graphics.DrawImage(bitmap,0,0);
}
}

Do both the implicit Region object and the Bitmap object get Disposed()?
 
A

Alex Meleta

Hi Martijn,

MM> using(e.Graphics.Clip=GetMyCustomRegion())
MM> Is the Region object returned by GetMyCustomRegion() succesfully Disposed()?

Yes, why not, reference is the same, it's kind of
using (Region tempRegion = (e.Graphics.Clip = <new Region()>))
{
e.Graphics.DrawImage(null, 0, 0);
}

MM> using(e.Graphics.Clip=GetMyCustomRegion())
MM> using(Bitmap bitmap=new Bitmap(34,55))
First for the bitmap, then for the region.

http://msdn2.microsoft.com/en-us/library/yh598w02(VS.80).aspx

Regards, Alex
[TechBlog] http://devkids.blogspot.com



MM> When I want to invoke Dispose() automatically on an object, I can
MM> use
MM>
MM> using(object)
MM> {
MM> //do something with object
MM> }
MM> and after the closing brace the object is Disposed(). But what get
MM> disposed when I use code like this
MM>
MM> override protected void OnPaint(PaintEventArgs e)
MM> {
MM> using(e.Graphics.Clip=GetMyCustomRegion())
MM> {
MM> e.Graphics.DrawImage(bitmap,0,0);
MM> }
MM> }
MM> Is the Region object returned by GetMyCustomRegion() succesfully
MM> Disposed()? How does it influence the Clip-property of the Graphics
MM> object?
MM>
MM> Also, can I nest 'using' statement like this:
MM>
MM> override protected void OnPaint(PaintEventArgs e)
MM> {
MM> using(e.Graphics.Clip=GetMyCustomRegion())
MM> using(Bitmap bitmap=new Bitmap(34,55))
MM> {
MM> e.Graphics.DrawImage(bitmap,0,0);
MM> }
MM> }
MM> Do both the implicit Region object and the Bitmap object get
MM> Disposed()?
MM>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top