天天看點

c#之using關鍵字

using 語句允許程式員指定使用資源的對象應當何時釋放資源。using 語句中使用的對象必須實作 IDisposable 接口。此接口提供了 Dispose 方法,該方法将釋放此對象的資源。

    ①可以在 using 語句之前聲明對象。
      Font font2 = new Font("Arial", 10.0f);
      using (font2)
      {
          // use font2
      }
    ②可以在 using 語句之中聲明對象。
      using (Font font2 = new Font("Arial", 10.0f))
      {
          // use font2
      }
    ③可以有多個對象與 using 語句一起使用,但是必須在 using 語句内部聲明這些對象。
        using (Font font3=new Font("Arial",10.0f), font4=new Font("Arial",10.0f))
      {
          // Use font3 and font4.
      }