zoukankan      html  css  js  c++  java
  • C# Using Statement + Using Keyword

    Using Statement

    The using statement obtains one or more resources, executes a statement, and then disposes of the resource. A using statement is translated into three parts:acquisition, usage, and disposal.

    Example:

    using (Font font1 = new Font("Arial", 10.0f))
    {
    }

    (

    Note: it's different from a direct block wrapping:

    {

      Font font1 = new Font();

      statements;

    }

    For using statement, it will finally call Dispose() of the Font while the direct block statement will do nothing for managed code here as GC will decide when to delete the GDI font object.

    )

    C# compiler will translate it to be:

    {
          Font font1= new Font("Arial", 10.0f);
          try {
                statement;
          }
          finally {
                if (font1!= null)((IDisposable)font1).Dispose();
          }
    }

    Using Keyword

    The using keyword has two uses:

    Using alias directives

    A using-alias-directive introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body.

     

    using identifier = namespace-or-type-name ;

    Within member declarations in a compilation unit or namespace body that contains a using-alias-directive, the identifier introduced by the using-alias-directive can be used to reference the given namespace or type.

    For example:

    namespace N1.N2
    {
    class A {}
    }
    namespace N3
    {
    using A = N1.N2.A;
    class B: A {}
    }

    Here, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B
    derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then
    referencing R.A:

     

    namespace N3
    {
    using R = N1.N2;
    class B: R.A {}
    }

    The identifier of a using-alias-directive must be unique within the declaration space of the compilation unit or namespace that immediately contains the using-alias-directive.

    Using namespace directives

    A using-namespace-directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification.

     

    using-namespace-directive:using namespace-name ;

    Within member declarations in compilation unit or namespace body that contains a using-namespace-directive, the types contained in the given namespace can be referenced directly.

    For example:

     namespace N1.N2
    {
    class A {}
    }
    namespace N3
    {
    using N1.N2;
    class B: A {}
    }

  • 相关阅读:
    第三十七节:系统证书管理和gRPC基于数字证书的认证和授权
    第三十六节:gRPC身份认证和授权(JWT模式 和 集成IDS4)
    第三十五节:gRPC拦截器、版本控制、安全性、日志集成
    第三十四节:.Proto文件剖析、gRPC的四种传输模式(一元和流式)和常用配置
    第三十三节:.Net Core下的gRPC详细介绍
    IIS7通过AppCmd.exe管理IIS
    MySql新增列的时候判断是否存在
    PowerDesigner从Excel导入表
    Java中Base64.encodeBase64URLSafe在C#的实现
    【产品方法论】字节跳动
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1703122.html
Copyright © 2011-2022 走看看