What are the uses of “using” in C#?

后端 未结 29 2932
有刺的猬
有刺的猬 2020-11-21 07:31

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

相关标签:
29条回答
  • 2020-11-21 07:46

    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.

    0 讨论(0)
  • 2020-11-21 07:46

    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.

    0 讨论(0)
  • 2020-11-21 07:46

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

    0 讨论(0)
  • 2020-11-21 07:47

    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 becomes out of the defined scope.
                }
    
    0 讨论(0)
  • 2020-11-21 07:48

    "using" can also be used to resolve name space conflicts. See http://www.davidarno.org/c-howtos/aliases-overcoming-name-conflicts/ for a short tutorial I wrote on the subject.

    0 讨论(0)
  • 2020-11-21 07:48
    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 
                }
        }
    
    0 讨论(0)
提交回复
热议问题