C# using用法
一、using指令
使用using指令在文件顶部引入命名空间,如
using System;
using System.IO;
二、using别名
用using为命名空间或类型定义别名,当引入的多个命名空间包含相同名字的类型时,需要使用该类型时,可以通过using为其指定别名,使代码更加简洁,避免冲突,例如:
using System;
using aTest = nameSpaceA.Test;
using bTest = nameSpaceB.Test; namespace @using
{
class Program
{
static void Main(string[] args)
{
aTest a = new aTest(); //aTest 代替 nameSpaceA.Test
a.fun();
bTest b = new bTest(); //bTest 代替 nameSpaceB.Test
b.fun();
Console.ReadKey();
}
}
} namespace nameSpaceA
{
public class Test
{
public void fun()
{
Console.WriteLine("this is test a");
}
} } namespace nameSpaceB
{
public class Test
{
public void fun()
{
Console.WriteLine("this is test b");
}
} }
输出:this is test a
this is test b
三、using语句
某些类型的非托管对象有数量限制或很耗费系统资源,在代码使用完它们后,尽可能快的释放它们时非常重要的。using语句有助于简化该过程并确保这些资源被适当的处置(dispose)。
它有两种使用形式。
1:
using (ResourceType Identifier = Expression ) Statement
圆括号中的代码分配资源,Statement是使用资源的代码
using语句会隐式产生处置该资源的代码,其步骤为:
a:分配资源
b:把Statement放进tyr块
c:创建资源的Dispose方法的调用,并把它放进finally块,例如:
using System;
using System.IO; namespace @using
{
class Program
{
static void Main(string[] args)
{
using (TextWriter tw = File.CreateText("test.txt"))
{
tw.Write("this is a test");
} using (TextReader tr = File.OpenText("test.txt"))
{
string input;
while ((input = tr.ReadLine()) != null)
{
Console.WriteLine(input);
}
}
Console.ReadKey();
}
}
}
输出:this is a test
2:
using (Expression) Statement
Expression 表示资源,Statement是使用资源,资源需要在using之前声明
TextWriter tw = File.CreateText("test.txt");
using(tw){......}
这种方式虽然可以确保对资源使用结束后调用Dispose方法,但不能防止在using语句已经释放了他的非托管资源之后使用该资源,可能导致不一致的状态,不推荐使用
C# using用法的更多相关文章
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
- 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...
- [转]thinkphp 模板显示display和assign的用法
thinkphp 模板显示display和assign的用法 $this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论 ...
随机推荐
- A SQL to insert continuous values
I need a table to store all the working days. I dont like loop, so I tried sql. The following is the ...
- 设计模式之外观模式(Facade Pattern)
一.什么是外观模式? 简单的说,外观模式是用来简化接口的. 通常,我们觉得一个子系统不好用,可能是因为它提供的外部接口太接近低层组件,让我们用起来感到很麻烦. 因为我们不需要知道内部细节,我们只想要一 ...
- C# Socket 实现WebSocket服务器端
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- python 发qq邮件
import smtplibfrom email.mime.text import MIMETextmsg_from = '979477675@qq.com' # 发送方邮箱passwd = 'irg ...
- 如何读取maven项目中的resources
建立一个maven web项目,project-name/src/main下面有3个目录,java.resources.webapp java中存放java源代码,package等 resources ...
- 使用session的监听器获取当前在线人数
1首先在web.xml中配置Session的监听器 2创建监听器并且继承HttpSessionListener 3.在jsp中导入监听器 4.获取当前在线人数 5.配置到公共网络(使用natapp的免 ...
- ElasticSearch的基本认识和基本操作
1.1. ElasticSearch(简称ES) ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案,其第一个版本于2010年2月出现在Git ...
- Flask 中的 Response
1.Flask中的HTTPResponse @app.route("/") # app中的路由route装饰器 def index(): # 视图函数 return "I ...
- (2)特征点匹配,并求旋转矩阵R和位移向量t
include头文件中有slamBase.h # pragma once // 各种头文件 // C++标准库 #include <fstream> #include <vector ...
- hdoj1072 Nightmare(bfs)
题目大意: 在迷宫中有一个炸弹,过六个单位时间就会爆炸,要你求一个起点到迷宫的终点的最短距离,迷宫中有时间重置器,当你走到这个格子,炸弹的爆炸时间重新置为0,迷宫中标识为墙壁的格子不能走,到达任意一个 ...