using (Customers customersAccess = new Customers())
                    {
                        result = customersAccess.UpdateCustomer(customer);
                    }
怎么理解?

解决方案 »

  1.   

    try{
        Customers customersAccess = new Customers();
        result = customersAccess.UpdateCustomer(customer);}
    finally{
        if(customersAccess!=null)
            ((IDisposable)customer).Dispose()
    }
      

  2.   

    using 语句定义一个范围,在此范围的末尾将处理对象。
    using (expression | type identifier = initializer) statement
    其中: 
    expression 
    希望在退出 using 语句时调用 Dispose 的表达式。 
    type 
    identifier 的类型。 
    identifier 
    type 类型的名称或标识符。定义一个以上 type 类型的 identifier 是可以的。在每一个 identifier = initializer 的前边都有一个逗号。 
    initializer 
    创建对象的表达式。 
    statement 
    嵌入的语句或要执行的语句。 
    备注
    在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句。
    实例化的对象必须实现 System.IDisposable 接口。
    示例
    // cs_using_statement.cs
    // compile with /reference:System.Drawing.dll
    using System.Drawing;
    class a
    {
       public static void Main()
       {
          using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial", 10.0f))
          {
             // use MyFont and MyFont2
          }   // compiler will call Dispose on MyFont and MyFont2      Font MyFont3 = new Font("Arial", 10.0f);
          using (MyFont3)
          {
             // use MyFont3
          }   // compiler will call Dispose on MyFont3   }
    }
      

  3.   

    也就是说,在这个范围内用到的实例对象,在结束这一小块代码后,编译器会自动地把它从内存中清除,是吗?
    而如果不用using 的话,则是应用程序结束后,才会从内存中清除。不知道我理解对不对。
      

  4.   

    对,如果不用using的话,GC进行垃圾回收
      

  5.   

    也就是说,在这个范围内用到的实例对象,在结束这一小块代码后,编译器会自动地把它从内存中清除,是吗?--- 是的。
    而如果不用using 的话,则是应用程序结束后,才会从内存中清除。-- 不是;GC来清除,什么时候我们不知道。