Am I using the "using" statement correctly?

2.1k views Asked by At

I would like to know whether my use of using is correct. In the using statmement, I am deciding whether or not the image should be used in the game.

Image imageOfEnemy;
using(imageOfEnemy=Bitmap.FromFile(path))
{
   // some stuff with the imageOfEnemy variable

}

From my understanding, I do not need to call Dispose now.

4

There are 4 answers

0
Peter Porfy On BEST ANSWER

using is a shorthand statement for IDisposable objects to simplify the try-finally block, with Dispose in the finally block.

http://msdn.microsoft.com/en-us/library/yh598w02.aspx

So yes, you don't have to call Dispose 'manually' in this case.

using System;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var example = new Example())
            {
                //do something
            }
        }
    }

    class Example : IDisposable
    {

        public void Dispose()
        {
            //Do something
        }
    }
}

the Main method will be this in MSIL:

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 2
    .locals init (
        [0] class ConsoleApplication3.Example example,
        [1] bool CS$4$0000)
    L_0000: nop 
    L_0001: newobj instance void ConsoleApplication3.Example::.ctor()
    L_0006: stloc.0 
    L_0007: nop 
    L_0008: nop 
    L_0009: leave.s L_001b
    L_000b: ldloc.0 
    L_000c: ldnull 
    L_000d: ceq 
    L_000f: stloc.1 
    L_0010: ldloc.1 
    L_0011: brtrue.s L_001a
    L_0013: ldloc.0 
    L_0014: callvirt instance void [mscorlib]System.IDisposable::Dispose()
    L_0019: nop 
    L_001a: endfinally 
    L_001b: nop 
    L_001c: ret 
    .try L_0007 to L_000b finally handler L_000b to L_001b
}

You can see the try-finally handler and the Dispose call even if you are new to MSIL.

4
Darin Dimitrov On

Yes, you are using it correctly. You don't need to dispose explicitly the Bitmap as it wil be disposed by the using statement. You could simplify even further by declaring the image variable inside:

using(var imageOfEnemy = Bitmap.FromFile(path))
{
    // some stuff with the imageOfEnemy variable
}

which is roughly equivalent to:

{
    var imageOfEnemy = Bitmap.FromFile(path);
    try 
    {
        // some stuff with the imageOfEnemy variable
    }
    finally 
    {
        ((IDisposable)imageOfEnemy).Dispose();
    }
}
2
Thunder On

Its correct, if Image implements IDisposable interface. The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

0
Thulani Chivandikwa On

Yes indeed you are using it the right way. Always note though that it is useful when the object you instatiate and use it on implements IDisposable. This is really a neat little thing that the CLR does for us and makes the code cleaner. VB.Net also now has support for this statement as well. From my knowledge there is no speed differences between a using statement of closin objects using a try catch block but i would recommend a usins statement over try-catch, cleaner code and you don't risk forgetting to dispose objects or failing to because you did not catch an exception