Saturday, February 12, 2011

What is the C# Using block and why should I use it?

What is the purpose of the Using block in C#? How is it different from a local variable?

  • Using calls Dispose() after the using-block is left, even if the code throws an exception.

    So you usually use using for classes that require cleaning up after them, like IO.

    So, this using block:

    using (MyClass mine = new MyClass())
    {
      mine.Action();
    }
    

    would do the same as:

    MyClass mine = new MyClass();
    try
    {
      mine.Action();
    }
    finally
    {
      if (mine != null)
        mine.Dispose();
    }
    

    Using using is way shorter and easier to read.

    From Sam
  • Placing code in a using block ensures that the objects are disposed (though not necessarily collected) as soon as control leaves the block.

    From DannySmurf
  • If the type implements IDisposable, it automatically disposes it.

    Given:

    public class SomeDisposableType : IDisposable
    {
       ...implmentation details...
    }
    

    These are equivalent:

    SomeDisposableType t = new SomeDisposableType();
    try {
        OperateOnType(t);
    }
    finally {
       t.Dispose();
    }
    
    using (SomeDisposableType u = new SomeDisposableType()) {
        OperateOnType(u);
    }
    

    The second is easier to read and maintain.

    Jon Skeet : Note that in general the finally block will check for nullity before calling Dispose. Not that it matters when you're calling a constructor, but...
    John : If you declare the variable outside the using block and then create a new instance in the using statement it may not dispose the item.
    From plinth
  • From MSDN:

    C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.

    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.

    In other words, the using statement tells .NET to release the object specified in the using block once it is no longer needed.

    tvanfosson : This gives the rationale for using "using", while @plinth shows what it actually does.
    Robert S. : Indeed. This is the answer to "What is the purpose of the Using block in C#?"
    raven : @tvanfosson: FYI, the ampersand character is meant to imply that you are directing your comment at a user, as I've done at the beginning of this comment. You don't need to prefix every reference to another user with this character as you did when you mentioned plinth.
    Scott Ferguson : @raven: FYI, that's not an ampersand, this is: & :)
    From Robert S.
  • It is really just some syntatic sugar that does not require you to explicity call Dispose on members that implement IDisposable.

  • using (B a = B())
    {
       DoSomethingWith(a);
    }
    

    is equivalent to

    B a = new A();
    try
    {
      DoSomethingWith(a);
    }
    finally
    {
       ((IDisposable)a).Dispose();
    }
    
  • The using statement obtains one or more resources, executes a statement, and then disposes of the resource.

0 comments:

Post a Comment