博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# using 用法
阅读量:4486 次
发布时间:2019-06-08

本文共 1027 字,大约阅读时间需要 3 分钟。

  ⑴ using 引入空间命名

 格式: using namespace 

     例如在创建C#的控制台程序时,会自动添加  

    using System;                                            using namespace

    using System.Collections.Generic;              #include <stdio>
    using System.Linq;                                     #include <math>
    using System.Text;                                      #include <string>
    using System.Threading.Tasks;                  #include<stdlib>

                (一大串啊)

   不过这样就可以直接使用命名空间中的类型,而不必指定详细的类型名称。using指令可以访问嵌套命名空间。(命名空间就是相当于C++中的头文件)

 

(2)创建命名空间别名

 

using为命名空间创建别名的用法规则为:

 

using alias = namespace | type;

 

其中namespace表示创建命名空间的别名;而type表示创建类型别名。例如,在.NET Office应用中,常常会引入Microsoft.Office.Interop.Word.dll程序集,在引入命名空间时为了避免繁琐的类型输入,我们通常为其创建别名如下:

 

using MSWord = Microsoft.Office.Interop.Word;

(3)资源清理

 

using 语句允许程序员指定使用资源的对象应当何时释放资源。using 语句中使用的对象必须实现 IDisposable 接口。此接口提供了 Dispose 方法,该方法将释放此对象的资源

代码 略

(4)支持初始化变量

using (Pen p1= new Pen(brushes.Black),p2=new Pen(brushes.blue){   //}

  

//类型必须相同

using ( IDisposable font =new Font ("vafeea e",10)pen = new Pen(Brushes.Black))   {   float size = (font as Font).Size;   Brush brush = (pen as Pen).Brush;   }  //类型不一定相同

  

 

 

转载于:https://www.cnblogs.com/zykh/p/7687952.html

你可能感兴趣的文章
url override and HttpSession implements session for form
查看>>
servlet乱码问题
查看>>
反射+特性实现 类和XML文档的序列化反序列化
查看>>
日常方法
查看>>
解决Ueditor在bootstarp 模态框中全屏问题
查看>>
POJ 1006 Biorhythms
查看>>
dubbo+zookeeper注册服务报错问题:No service registed on zookeeper
查看>>
极验滑动验证登录
查看>>
求多个数的质因子
查看>>
laravel的orm作用
查看>>
文件锁
查看>>
props和state
查看>>
LeetCode:Unique Paths I II
查看>>
AcWing 兔子与兔子
查看>>
洛谷 P3871 [TJOI2010]中位数
查看>>
洛谷 P2073 送花
查看>>
洛谷 P1801 黑匣子_NOI导刊2010提高(06)
查看>>
洛谷 P1503 鬼子进村
查看>>
E. The shortest problem
查看>>
进制转换——9018——1065
查看>>