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

后端 未结 29 2728
有刺的猬
有刺的猬 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:40

    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);
              }
          }
       }
    
    }
    
    0 讨论(0)
  • 2020-11-21 07:41

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

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

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

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

    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;
    
    0 讨论(0)
  • 2020-11-21 07:45

    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.

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

    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 MSIL. And the using statement gets translated into a try and finally block. This is how the using statement is represented in IL. 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.

    0 讨论(0)
提交回复
热议问题