• 你知道using的用法吗?


      在c#中我们可能经常看到using System,看多了就麻木了,可能有些朋友不知道using的其他的用法。这里我帮大家总结一下using的各个用法。

      首先打开MSDN帮助,查找using可以找到解释如下:

      using 关键字有两个主要用途:

    • 作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型。请参见 using 指令。
    • 作为语句,用于定义一个范围,在此范围的末尾将释放对象。请参见 using 语句。

      好了我们看到了MSDN给了我们答案,其中using只要有两种用法一种是指令、另一种是语句。

      那我们从指令开始吧,而在MSDN中指令又有两种用法

      1.允许在命名空间中使用类型,这样,您就不必在该命名空间中限定某个类型的使用:

      using System.Text;

      2为命名空间或类型创建别名。

      using Project = PC.MyCompany.Project;

      (1.1)第一种我们在熟悉不过了,但是你知道vs的编译过程吗。当我们创建一个控制台应用程序,系统为我们生成了很多个默认的using空间。

      例如:

    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;

     

      但是当我们写一个hello world 程序的时候,真正用到的只有using System这个空间。可能你会说那我们引用这么多空间干嘛,不会占用资源吗?这里我来告诉你答案。虽然我们引用了很多多余的空间但是我们的vs却帮我们在内存中引用了它需要的那些。当然从我们角度看,有没有办法去掉那些我们不需要的命名空间呢?这里我告诉你一种办法。当我们做好整个项目的时候我们在using的区域内右击-----组织using------移除和排序,这样就可以去掉那些不需要的了。

      (1.2)为命名空间或类型创建别名。这个用到的不是很多,但是讲到了那我们就详细点。当我们在一个命名空间内调用一个类的时候,没有有问题的。但是当我们调用在两个不同的命名空间内的相同名字的类名时,这时你是否想到办法呢。这就是别名的用处了,我们给两个空间分别取个别名,然后别名.类名,这样不是分开他们了吗。

    using别名例子
     1 // cs_using_directive2.cs
    2 // Using directive.
    3 using System;
    4 // Using alias for a class.
    5 using AliasToMyClass = NameSpace1.MyClass;
    6
    7 namespace NameSpace1
    8 {
    9 public class MyClass
    10 {
    11 public override string ToString()
    12 {
    13 return "You are in NameSpace1.MyClass";
    14 }
    15 }
    16 }
    17
    18 namespace NameSpace2
    19 {
    20 class MyClass
    21 {
    22 }
    23 }
    24
    25 namespace NameSpace3
    26 {
    27 // Using directive:
    28 using NameSpace1;
    29 // Using directive:
    30 using NameSpace2;
    31
    32 class MainClass
    33 {
    34 static void Main()
    35 {
    36 AliasToMyClass somevar = new AliasToMyClass();
    37 Console.WriteLine(somevar);
    38 }
    39 }
    40 }
    41
    42

    运行结果:

    You are in NameSpace1.MyClass
    

      (2)using (Font font3 = new Font("Arial", 10.0f),font4 = new Font("Arial", 10.0f))

      首先Font 是访问非托管资源(本例中为文件句柄和设备上下文)的托管类型的示例。有许多其他类别的非托管资源和封装这些资源的类库类型。所有这些类型都必须实现 IDisposable 接口。

      按照规则,当使用 IDisposable 对象时,应在 using 语句中声明和实例化此对象。using 语句按照正确的方式调用对象上的 Dispose 方法,并(在您按照前面所示方式使用它时)会导致在调用 Dispose 时对象自身处于范围之外。在 using 块中,对象是只读的并且无法进行修改或重新分配。

       using 语句确保调用 Dispose,即使在调用对象上的方法时发生异常也是如此。通过将对象放入 try 块中,并在调用 finally 块中的 Dispose,可以获得相同的结果;实际上,这就是编译器转换 using 语句的方式。前面的代码示例在编译时将扩展到以下代码(请注意,使用额外的大括号为对象创建有限范围):

      这句你理解了吗?那我们从IL代码看看using语句的实质。

     1 .method private hidebysig static void  Main(string[] args) cil managed
    2 {
    3 .entrypoint
    4 // 代码大小 80 (0x50)
    5 .maxstack 3
    6 .locals init ([0] class [System.Drawing]System.Drawing.Font font3,
    7 [1] class [System.Drawing]System.Drawing.Font font4,
    8 [2] bool CS$4$0000)
    9 IL_0000: nop
    10 IL_0001: ldstr "Arial"
    11 IL_0006: ldc.r4 10.
    12 IL_000b: newobj instance void [System.Drawing]System.Drawing.Font::.ctor(string,
    13 float32)
    14 IL_0010: stloc.0
    15 .try
    16 {
    17 IL_0011: ldstr "Arial"
    18 IL_0016: ldc.r4 10.
    19 IL_001b: newobj instance void [System.Drawing]System.Drawing.Font::.ctor(string,
    20 float32)
    21 IL_0020: stloc.1
    22 .try
    23 {
    24 IL_0021: nop
    25 IL_0022: nop
    26 IL_0023: leave.s IL_0035
    27 } // end .try
    28 finally
    29 {
    30 IL_0025: ldloc.1
    31 IL_0026: ldnull
    32 IL_0027: ceq
    33 IL_0029: stloc.2
    34 IL_002a: ldloc.2
    35 IL_002b: brtrue.s IL_0034
    36 IL_002d: ldloc.1
    37 IL_002e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
    38 IL_0033: nop
    39 IL_0034: endfinally
    40 } // end handler
    41 IL_0035: nop
    42 IL_0036: leave.s IL_0048
    43 } // end .try
    44 finally
    45 {
    46 IL_0038: ldloc.0
    47 IL_0039: ldnull
    48 IL_003a: ceq
    49 IL_003c: stloc.2
    50 IL_003d: ldloc.2
    51 IL_003e: brtrue.s IL_0047
    52 IL_0040: ldloc.0
    53 IL_0041: callvirt instance void [mscorlib]System.IDisposable::Dispose()
    54 IL_0046: nop
    55 IL_0047: endfinally
    56 } // end handler
    57 IL_0048: nop
    58 IL_0049: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
    59 IL_004e: pop
    60 IL_004f: ret
    61 } // end of method Program::Main

      可以看到其实using对应的是try、finally来处理。

      所有上面的句子等同于

     1  Font font3 = new Font("Arial", 10.0f);
    2 try
    3 {
    4 Font font4 = new Font("Arial", 10.0f);
    5 try
    6 {
    7 }
    8 finally
    9 {
    10 if (font4 != null)
    11 ((IDisposable)font4).Dispose();
    12 }
    13 }
    14 finally
    15 {
    16 if (font3 != null)
    17 ((IDisposable)font3).Dispose();
    18 }

     

    虽然using语句使用很简单,但是还是需要有几点注意的地方。

    1. Dispose方法用于清理对象封装的非托管资源,而不是释放对象的内存,对象的内存永远有垃圾回收器控制。
    2. 程序在达到using语句末尾时退出using块,而如果到达语句末尾之前引入异常则有可能提前退出。
    3. Using中初始化的对象,可以在using语句之前声明。

    如:

    Font font3 = new Font("Arial", 10.0f);
    Using(font3){
    }

    4.要使用的类必须实现IDisposable接口。

    Stallman 先生认为最大的快乐是让自己发展的软件让大家来使用了!

  • 相关阅读:
    软件测试理论提炼
    测试左移与右移
    RFS工具基础学习
    机遇——沃特·马龙
    SVN (Subversion+tortoiseSVN)使用手册
    MongoDB基本使用
    win7系统快捷键操作大全
    SQL Server Profiler使用方法
    转载《soapUI 学习日志》
    HTMLTestRunner中文测试报告
  • 原文地址:https://www.cnblogs.com/Jimmy009/p/2358468.html
Copyright © 2020-2023  润新知