User kokos answered the wonderful Hidden Features of C# question by mentioning the using
keyword. Can you elaborate on that? What are the uses of using
?

- 30,738
- 21
- 105
- 131

- 3,587
- 3
- 17
- 5
-
It's a C# way of supporting the RAII idiom: http://www.hackcraft.net/raii/ – Nemanja Trifunovic Sep 16 '08 at 18:35
-
1You can use for objects those have implemented IDispose interface. Using will call Dispose method when that object goes out of scope. It guarantees to call Dispose even if any exception occurs. It works like a finally clause and execute Dispose. – CharithJ Jul 04 '15 at 01:11
29 Answers
The reason for the using
statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens.
As in Understanding the 'using' statement in C# (codeproject) and Using objects that implement IDisposable (microsoft), the C# compiler converts
using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}
to
{ // Limits scope of myRes
MyResource myRes= new MyResource();
try
{
myRes.DoSomething();
}
finally
{
// Check for a null resource.
if (myRes != null)
// Call the object's Dispose method.
((IDisposable)myRes).Dispose();
}
}
C# 8 introduces a new syntax, named "using declarations":
A using declaration is a variable declaration preceded by the using keyword. It tells the compiler that the variable being declared should be disposed at the end of the enclosing scope.
So the equivalent code of above would be:
using var myRes = new MyResource();
myRes.DoSomething();
And when control leaves the containing scope (usually a method, but it can also be a code block), myRes
will be disposed.
-
143Note that it's not necessarily a matter of the object being disposed _correctly_, but more of whether it is disposed in a timely manner. Objects implementing IDisposable which hold on to unmanaged resources like streams and file handles will also implement a finalizer that will ensure that Dispose is called during garbage collection. The problem is that GC might not happen for a relatively long time. `using` makes sure that `Dispose` is called once you're through with the object. – John Saunders Jan 11 '12 at 18:53
-
1Please note that the generated code is a bit different when `MyRessource` is a struct. There is obviously no test for nullity, but also no boxing to `IDisposable`. A constrained virtual call is emitted. – Romain Verdier May 03 '14 at 14:31
-
6Why is nobody mentioning that using is also used to import namespaces? – Kyle Delaney Jan 12 '17 at 23:35
-
3Note that if you write directly the second version of the code, the result is not the same. If you use the `using`, the variable built inside it is readonly. There is no way to achieve this for local variables without the `using` statement. – Massimiliano Kraus Nov 24 '17 at 16:08
-
For someone who comes from the land of python, is this equivalent to something like `with open(filename) as f: # blah`? – Chromium Jul 18 '18 at 07:09
-
2
Since a lot of people still do:
using (System.IO.StreamReader r = new System.IO.StreamReader(""))
using (System.IO.StreamReader r2 = new System.IO.StreamReader("")) {
//code
}
I guess a lot of people still don't know that you can do:
using (System.IO.StreamReader r = new System.IO.StreamReader(""), r2 = new System.IO.StreamReader("")) {
//code
}

- 5
- 3

- 6,006
- 7
- 38
- 48
-
2Is it possible to use multiple objects of different types in a single using statement? – Agnel Kurian Apr 24 '13 at 12:12
-
14@AgnelKurian No: "error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement" – David Sykes Jan 23 '14 at 09:00
-
21
-
I actually didn't know I can write two using statemens before a single block of code (would nest them every time). – kub1x Oct 12 '18 at 07:58
Things like this:
using (var conn = new SqlConnection("connection string"))
{
conn.Open();
// Execute SQL statement here on the connection you created
}
This SqlConnection
will be closed without needing to explicitly call the .Close()
function, and this will happen even if an exception is thrown, without the need for a try
/catch
/finally
.

- 1,122
- 15
- 31

- 399,467
- 113
- 570
- 794
-
1what if I'm using a "using" inside a method, and I return in the middle of a using. Is there any problem? – francisco_ssb Mar 04 '16 at 14:51
-
1There is not problem. In the example here, the connection will still be closed, even if you `return` from the middle of the `using` block. – Joel Coehoorn Mar 04 '16 at 18:00
using can be used to call IDisposable. It can also be used to alias types.
using (SqlConnection cnn = new SqlConnection()) { /* Code */}
using f1 = System.Windows.Forms.Form;

- 30,738
- 21
- 105
- 131

- 9,695
- 6
- 32
- 43
using, in the sense of
using (var foo = new Bar())
{
Baz();
}
Is actually shorthand for a try/finally block. It is equivalent to the code:
var foo = new Bar();
try
{
Baz();
}
finally
{
foo.Dispose();
}
You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:
using (new Scope(() => IsWorking = false))
{
IsWorking = true;
MundaneYetDangerousWork();
}
You can read more about our solution and how we derived it here.

- 30,738
- 21
- 105
- 131

- 2,665
- 1
- 16
- 23
Microsoft documentation states that using has a double function (https://msdn.microsoft.com/en-us/library/zhdeatwt.aspx), both as a directive and in statements. As a statement, as it was pointed out here in other answers, the keyword is basically syntactic sugar to determine a scope to dispose an IDisposable object. As a directive, it is routinely used to import namespaces and types. Also as a directive, you can create aliases for namespaces and types, as pointed out in the book "C# 5.0 In a Nutshell: The Definitive Guide" (http://www.amazon.com/5-0-Nutshell-The-Definitive-Reference-ebook/dp/B008E6I1K8), by Joseph and Ben Albahari. One example:
namespace HelloWorld
{
using AppFunc = Func<IDictionary<DateTime, string>, List<string>>;
public class Startup
{
public static AppFunc OrderEvents()
{
AppFunc appFunc = (IDictionary<DateTime, string> events) =>
{
if ((events != null) && (events.Count > 0))
{
List<string> result = events.OrderBy(ev => ev.Key)
.Select(ev => ev.Value)
.ToList();
return result;
}
throw new ArgumentException("Event dictionary is null or empty.");
};
return appFunc;
}
}
}
This is something to adopt wisely, since the abuse of this practice can hurt the clarity of one's code. There is a nice explanation on C# aliases, also mentioning pros and cons, in DotNetPearls (http://www.dotnetperls.com/using-alias).

- 615
- 5
- 10
-
5Not gonna lie: I *hate* the use of `using` as an alias tool. It confuses me when reading the code -- I already know that `System.Collections` exists and has the `IEnumerable
` class. Using an alias to call it something else obfuscates it for me. I see `using FooCollection = IEnumerable – Ari Roth Dec 28 '17 at 19:08` as a way to make later developers read the code and think, "What the hell is a `FooCollection` and why isn't there a class for it somewhere?" I never use it and would discourage its use. But that might just be me. -
1Addendum: I'll concede that there might be a use for it occasionally, as in your example where you use it to define a delegate. But I'd argue those are relatively rare. – Ari Roth Dec 28 '17 at 19:10
I've used it a lot in the past to work with input and output streams. You can nest them nicely and it takes away a lot of the potential problems you usually run into (by automatically calling dispose). For example:
using (FileStream fs = new FileStream("c:\file.txt", FileMode.Open))
{
using (BufferedStream bs = new BufferedStream(fs))
{
using (System.IO.StreamReader sr = new StreamReader(bs))
{
string output = sr.ReadToEnd();
}
}
}

- 6,666
- 6
- 44
- 54
Just adding a little something that I was surprised did not come up. The most interesting feature of using (in my opinion) is that no matter how you exit the using block, it will always dispose the object. This includes returns and exceptions.
using (var db = new DbContext())
{
if(db.State == State.Closed)
throw new Exception("Database connection is closed.");
return db.Something.ToList();
}
It doesn't matter if the exception is thrown or the list is returned. The DbContext object will always be disposed.

- 30,738
- 21
- 105
- 131

- 2,909
- 1
- 22
- 36
Another great use of using is when instantiating a modal dialog.
Using frm as new Form1
Form1.ShowDialog
' Do stuff here
End Using

- 30,738
- 21
- 105
- 131

- 158
- 1
- 2
- 8
-
3
-
The question was about C#. Does it work the same in VB.NET or does it just look that way? In which version of VB.NET (and corresponding version of Visual Studio) was it introduced? – Peter Mortensen Sep 02 '21 at 12:50
You can make use of the alias namespace by way of the following example:
using LegacyEntities = CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects;
This is called a using alias directive as as you can see, it can be used to hide long-winded references should you want to make it obvious in your code what you are referring to e.g.
LegacyEntities.Account
instead of
CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects.Account
or simply
Account // It is not obvious this is a legacy entity

- 3,084
- 1
- 27
- 45
Interestingly, you can also use the using/IDisposable pattern for other interesting things (such as the other point of the way that Rhino Mocks uses it). Basically, you can take advantage of the fact that the compiler will always call .Dispose on the "used" object. If you have something that needs to happen after a certain operation ... something that has a definite start and end ... then you can simply make an IDisposable class that starts the operation in the constructor, and then finishes in the Dispose method.
This allows you to use the really nice using syntax to denote the explicit start and end of said operation. This is also how the System.Transactions stuff works.

- 46,929
- 26
- 130
- 185
In conclusion, when you use a local variable of a type that implements IDisposable
, always, without exception, use using
1.
If you use nonlocal IDisposable
variables, then always implement the IDisposable
pattern.
Two simple rules, no exception1. Preventing resource leaks otherwise is a real pain in the *ss.
1): The only exception is – when you're handling exceptions. It might then be less code to call Dispose
explicitly in the finally
block.

- 530,221
- 131
- 937
- 1,214
public class ClassA:IDisposable
{
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
}
public void fn_Data()
{
using (ClassA ObjectName = new ClassA())
{
// Use objectName
}
}

- 30,738
- 21
- 105
- 131

- 665
- 6
- 8
-
An explanation would be in order. E.g., what is the idea/gist? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/13175552/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Sep 02 '21 at 12:51
There are two usages of the using
keyword in C# as follows.
As a directive
Generally we use the
using
keyword to add namespaces in code-behind and class files. Then it makes available all the classes, interfaces and abstract classes and their methods and properties in the current page.Example:
using System.IO;
As a statement
This is another way to use the
using
keyword in C#. It plays a vital role in improving performance in garbage collection.The
using
statement ensures that Dispose() is called even if an exception occurs when you are creating objects and calling methods, properties and so on. Dispose() is a method that is present in the IDisposable interface that helps to implement custom garbage collection. In other words if I am doing some database operation (Insert, Update, Delete) but somehow an exception occurs then here the using statement closes the connection automatically. No need to call the connection Close() method explicitly.Another important factor is that it helps in Connection Pooling. Connection Pooling in .NET helps to eliminate the closing of a database connection multiple times. It sends the connection object to a pool for future use (next database call). The next time a database connection is called from your application the connection pool fetches the objects available in the pool. So it helps to improve the performance of the application. So when we use the using statement the controller sends the object to the connection pool automatically, there is no need to call the Close() and Dispose() methods explicitly.
You can do the same as what the using statement is doing by using try-catch block and call the Dispose() inside the finally block explicitly. But the using statement does the calls automatically to make the code cleaner and more elegant. Within the using block, the object is read-only and cannot be modified or reassigned.
Example:
string connString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"; using (SqlConnection conn = new SqlConnection(connString)) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT CustomerId, CompanyName FROM Customers"; conn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) Console.WriteLine("{0}\t{1}", dr.GetString(0), dr.GetString(1)); } }
In the preceding code I am not closing any connection; it will close automatically. The using
statement will call conn.Close() automatically due to the using
statement (using (SqlConnection conn = new SqlConnection(connString)
) and the same for a SqlDataReader object. And also if any exception occurs it will close the connection automatically.
For more information, see Usage and Importance of Using in C#.

- 30,738
- 21
- 105
- 131

- 3,304
- 2
- 32
- 43
When using ADO.NET you can use the keywork for things like your connection object or reader object. That way when the code block completes it will automatically dispose of your connection.

- 47,650
- 10
- 49
- 73
-
2I would just add that the code block doesn't even have to complete. A using block will dispose of the resource even in the event of an unhandled exception. – harpo Sep 16 '08 at 18:27
-
Just to further clarify, it's a way of making sure the Garbage Collector takes care of your allocations when you want it to, instead of doing it when *it* wants to. – moswald Sep 16 '08 at 18:28
"using" can also be used to resolve namespace conflicts.
See http://www.davidarno.org/c-howtos/aliases-overcoming-name-conflicts/ for a short tutorial I wrote on the subject.

- 30,738
- 21
- 105
- 131

- 42,717
- 16
- 86
- 131
-
The link is broken: *"Hmm. We’re having trouble finding that site. We can’t connect to the server at www.davidarno.org."* – Peter Mortensen Sep 02 '21 at 12:41
using is used when you have a resource that you want disposed after it's been used.
For instance if you allocate a File resource and only need to use it in one section of code for a little reading or writing, using is helpful for disposing of the File resource as soon as your done.
The resource being used needs to implement IDisposable to work properly.
Example:
using (File file = new File (parameters))
{
// Code to do stuff with the file
}

- 30,738
- 21
- 105
- 131

- 3,212
- 6
- 34
- 44
Everything outside the curly brackets is disposed, so it is great to dispose your objects if you are not using them. This is so because if you have a SqlDataAdapter object and you are using it only once in the application life cycle and you are filling just one dataset and you don't need it anymore, you can use the code:
using(SqlDataAdapter adapter_object = new SqlDataAdapter(sql_command_parameter))
{
// do stuff
} // here adapter_object is disposed automatically

- 1,060
- 2
- 8
- 17
The using statement provides a convenience mechanism to correctly use IDisposable objects. As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement.
The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
This comes from here.

- 30,738
- 21
- 105
- 131

- 89
- 2
- 10
-
The link is not really a link (too unspecific), but a generic search page (*"654,563 results for "using'"*). – Peter Mortensen Sep 02 '21 at 12:59
For me the name "using" is a little bit confusing, because is can be a directive to import a Namespace or a statement (like the one discussed here) for error handling.
A different name for error handling would've been nice, and maybe a somehow more obvious one.

- 839
- 8
- 13
It also can be used for creating scopes for Example:
class LoggerScope:IDisposable {
static ThreadLocal<LoggerScope> threadScope =
new ThreadLocal<LoggerScope>();
private LoggerScope previous;
public static LoggerScope Current=> threadScope.Value;
public bool WithTime{get;}
public LoggerScope(bool withTime){
previous = threadScope.Value;
threadScope.Value = this;
WithTime=withTime;
}
public void Dispose(){
threadScope.Value = previous;
}
}
class Program {
public static void Main(params string[] args){
new Program().Run();
}
public void Run(){
log("something happend!");
using(new LoggerScope(false)){
log("the quick brown fox jumps over the lazy dog!");
using(new LoggerScope(true)){
log("nested scope!");
}
}
}
void log(string message){
if(LoggerScope.Current!=null){
Console.WriteLine(message);
if(LoggerScope.Current.WithTime){
Console.WriteLine(DateTime.Now);
}
}
}
}

- 1,080
- 10
- 19
The using statement tells .NET to release the object specified in the using block once it is no longer needed.
So you should use the 'using' block for classes that require cleaning up after them, like System.IO types.

- 30,738
- 21
- 105
- 131

- 220
- 2
- 6
When you use using, it will call the Dispose() method on the object at the end of the using's scope. So you can have quite a bit of great cleanup code in your Dispose() method.
A bullet point:
If you implement IDisposable, make sure you call GC.SuppressFinalize() in your Dispose() implementation, as otherwise automatic garbage collection will try to come along and Finalize it at some point, which at the least would be a waste of resources if you've already Dispose()d of it.

- 30,738
- 21
- 105
- 131

- 5,242
- 7
- 33
- 36
-
It has an indirect effect. Because you've disposed of the object explicitly, it does not require finalization and can therefore be GC'd earlier. – Kent Boogaart Sep 16 '08 at 18:38
The using keyword defines the scope for the object and then disposes of the object when the scope is complete. For example.
using (Font font2 = new Font("Arial", 10.0f))
{
// Use font2
}
See here for the MSDN article on the C# using keyword.

- 30,738
- 21
- 105
- 131

- 72,212
- 42
- 129
- 156
-
The link is effectively broken: *"Visual Studio 2005 Retired documentation"* – Peter Mortensen Sep 02 '21 at 12:43
Not that it is ultra important, but using can also be used to change resources on the fly.
Yes, disposable as mentioned earlier, but perhaps specifically you don't want the resources they mismatch with other resources during the rest of your execution. So you want to dispose of it so it doesn't interfere elsewhere.

- 30,738
- 21
- 105
- 131
Another example of a reasonable use in which the object is immediately disposed:
using (IDataReader myReader = DataFunctions.ExecuteReader(CommandType.Text, sql.ToString(), dp.Parameters, myConnectionString))
{
while (myReader.Read())
{
MyObject theObject = new MyObject();
theObject.PublicProperty = myReader.GetString(0);
myCollection.Add(theObject);
}
}

- 628
- 8
- 17
using as a statement automatically calls the dispose on the specified object. The object must implement the IDisposable interface. It is possible to use several objects in one statement as long as they are of the same type.
The CLR converts your code into CIL. And the using statement gets translated into a try and finally block. This is how the using statement is represented in CIL. A using statement is translated into three parts: acquisition, usage, and disposal. The resource is first acquired, then the usage is enclosed in a try statement with a finally clause. The object then gets disposed in the finally clause.

- 30,738
- 21
- 105
- 131

- 1,255
- 1
- 12
- 26
The Rhino Mocks Record-playback Syntax makes an interesting use of using
.
-
its basically the same as calling Playback and Verify all it just does that in the Dispose() methods – George Mauer Sep 16 '08 at 18:37
The using clause is used to define the scope for the particular variable.
For example:
Using(SqlConnection conn = new SqlConnection(ConnectionString)
{
Conn.Open()
// Execute SQL statements here.
// You do not have to close the connection explicitly
// here as "USING" will close the connection once the
// object Conn goes out of the defined scope.
}

- 30,738
- 21
- 105
- 131

- 19
- 3
-
This could misslead someone, using is for disposing objects. Maybe you are confusing this with code block, if you want to limit the scope of a variable you can use nested code block for that: public static void Main(params string[] args){ { //nested code block } } – luiseduardohd Aug 15 '19 at 22:46
-
@luise.. right. 1. Using is used to release the memory consumed by the objects. – Riya Patil Feb 16 '21 at 22:39