What are the uses of "using" in C#?
Asked Answered
E

29

359

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?

Effector answered 16/9, 2008 at 18:23 Comment(2)
It's a C# way of supporting the RAII idiom: hackcraft.net/raiiChenoweth
You 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.Elisabethelisabethville
A
538

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.

Accipiter answered 16/9, 2008 at 18:30 Comment(6)
Note 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.Leapt
Please 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.Shutz
Why is nobody mentioning that using is also used to import namespaces?Ruben
Note 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.Diplomat
For someone who comes from the land of python, is this equivalent to something like with open(filename) as f: # blah?Hett
@JohnSaunders Besides, the finalizer is not guaranteed to be called.Brazzaville
P
138

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
}
Pout answered 16/9, 2008 at 19:11 Comment(4)
Is it possible to use multiple objects of different types in a single using statement?Sob
@AgnelKurian No: "error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement"Auriferous
How does this answer the question?Memling
I actually didn't know I can write two using statemens before a single block of code (would nest them every time).Carleencarlen
U
101

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.

Unwonted answered 16/9, 2008 at 18:26 Comment(2)
what if I'm using a "using" inside a method, and I return in the middle of a using. Is there any problem?Mallen
There is not problem. In the example here, the connection will still be closed, even if you return from the middle of the using block.Unwonted
E
32

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;
Egwan answered 16/9, 2008 at 18:26 Comment(1)
Not in this order though ;)Bazooka
B
22

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.

Blankenship answered 15/10, 2008 at 14:35 Comment(0)
R
12

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).

Recall answered 25/12, 2015 at 18:30 Comment(2)
Not 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<T> class. Using an alias to call it something else obfuscates it for me. I see using FooCollection = IEnumerable<Foo> 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.Clodhopping
Addendum: 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.Clodhopping
D
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();
                }
            }
        }
Decemvirate answered 16/9, 2008 at 18:34 Comment(0)
S
8

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.

Shooin answered 27/4, 2015 at 13:46 Comment(0)
D
6

Another great use of using is when instantiating a modal dialog.

Using frm as new Form1

    Form1.ShowDialog

    ' Do stuff here

End Using
Drypoint answered 16/9, 2008 at 19:58 Comment(2)
Did you mean frm.ShowDialog?Casket
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?Puparium
S
5

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
Spathose answered 10/4, 2014 at 17:27 Comment(0)
P
4

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.

Poltroon answered 16/9, 2008 at 18:32 Comment(0)
D
3

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.

Downrange answered 16/9, 2008 at 18:25 Comment(2)
I 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.Overstrain
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.Bear
O
3

"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.

Oval answered 16/9, 2008 at 18:27 Comment(1)
The link is broken: "Hmm. We’re having trouble finding that site. We can’t connect to the server at www.davidarno.org."Puparium
H
3

When you use a local variable of a type that implements IDisposable, always, without exception, use using1.

If you use nonlocal IDisposable variables, then always implement the IDisposable pattern.

Two simple rules, with possibly one exception1. Preventing resource leaks otherwise is a real pain.


1): The only exception is – when you're handling exceptions. It might then be less code to call Dispose explicitly in the finally block.

Hagberry answered 16/9, 2008 at 19:48 Comment(1)
"Always" is too strong. It does not apply to types that abuse the IDisposable pattern for things other than cleaning up unmanaged resources. For example, the Dataflow library uses IDisposable objects to represent the links between blocks, and calling Dispose() is how you break that link. Automatic disposal of those links may not be what you want.Salvucci
Y
3
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
    }
}
Yoho answered 1/11, 2012 at 10:54 Comment(1)
An explanation would be in order. E.g., what is the idea/gist? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).Puparium
C
3

There are two usages of the using keyword in C# as follows.

  1. 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;
    
  2. 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#.

Convenient answered 30/7, 2018 at 8:49 Comment(0)
D
2

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
}
Difficult answered 16/9, 2008 at 18:30 Comment(0)
C
1

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.

Careless answered 16/9, 2008 at 18:26 Comment(1)
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.Pastel
R
1

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.

Rackrent answered 16/9, 2008 at 18:28 Comment(1)
The link is effectively broken: "Visual Studio 2005 Retired documentation"Puparium
C
1

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.

Cheryllches answered 16/9, 2008 at 18:47 Comment(0)
F
1

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);
    }
}
Fright answered 16/9, 2008 at 19:52 Comment(0)
P
1

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
Pinter answered 15/10, 2008 at 14:43 Comment(0)
L
1

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.

Luciolucita answered 2/4, 2015 at 7:15 Comment(1)
The link is not really a link (too unspecific), but a generic search page ("654,563 results for "using'").Puparium
P
1

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.

Proximate answered 27/4, 2015 at 13:33 Comment(0)
M
1

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);
          }
      }
   }

}
Muscovado answered 4/1, 2017 at 11:51 Comment(0)
K
0

The Rhino Mocks Record-playback Syntax makes an interesting use of using.

Ketchan answered 16/9, 2008 at 18:26 Comment(1)
its basically the same as calling Playback and Verify all it just does that in the Dispose() methodsRiflery
K
0

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.

Khaddar answered 8/3, 2018 at 6:16 Comment(0)
W
0

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.

Wary answered 11/5, 2018 at 13:16 Comment(0)
H
-3

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.
}
Honourable answered 28/11, 2013 at 17:4 Comment(2)
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 } }Rajah
@luise.. right. 1. Using is used to release the memory consumed by the objects.Honourable

© 2022 - 2024 — McMap. All rights reserved.