tags:

views:

2460

answers:

7

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

+9  A: 

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.

Sam
+2  A: 

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

DannySmurf
+37  A: 

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.

plinth
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...
Jon Skeet
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.
John
+3  A: 

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.

Robert S.
This gives the rationale for using "using", while @plinth shows what it actually does.
tvanfosson
Indeed. This is the answer to "What is the purpose of the Using block in C#?"
Robert S.
@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.
raven
Scott Ferguson
A: 

It is really just some syntatic sugar that does not require you to explicity call Dispose on members that implement IDisposable.

SaaS Developer
+1  A: 
using (B a = B())
{
   DoSomethingWith(a);
}

is equivalent to

B a = new A();
try
{
  DoSomethingWith(a);
}
finally
{
   ((IDisposable)a).Dispose();
}
Bert Huijben
+1  A: 

The using statement obtains one or more resources, executes a statement, and then disposes of the resource.

Meera Tolia