Is all unmanaged code object will dispose while using the "using" Keyword ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span>
<span>Please look at below examples. In example 1 and 2 bitmap1 will dispose automatically when exception occured and method goes out of scope. but My doubt is in example 2 i declared another one line (Bitmap
bitmap2 = <span>new Bitmap(<span>100, <span>100);<span>), whether the bitmap2 would dispose automatically when an exception occured?

<span>2. or i need to declare the bitmap 2 in "using" statement?
<span>3. or if it would dispose bitmap2 means , whether it will dispose all unmanaged code which is declared inside the block?
<span>Example 1 :
<span>private<span>static<span>void Main (<span>string[] args)<br/>
{<br/>
<span><span>using (Bitmap bitmap1 =
<span>new Bitmap(<span>100, <span>100))<br/>
{<br/>
Console.WriteLine(<span>"<span>Width: {0}, Height: {1}", bitmap1.Width, bitmap1.Height);<br/>
}<br/>
Console.ReadLine();<br/>
}
Example 2:
<span>private<span>static<span>void Main (<span>string[] args)<br/>
{<br/>
<span><span>using (Bitmap bitmap1 =
<span>new Bitmap(<span>100, <span>100))<br/>
{
Bitmap
bitmap2 = <span>new Bitmap(<span>100, <span>100);
Console.WriteLine(<span>"<span>Width: {0}, Height: {1}", bitmap1.Width, bitmap1.Height);<br/>
}<br/>
Console.ReadLine();<br/>
}

Please anyone clear my doubt?
<
Manikandan Murugeshan
<br/>

View the full article
 
Back
Top