强大的using语句
来源:互联网 发布:c 多线程编程面试题 编辑:程序博客网 时间:2024/05/21 01:47
using web;
using ……;
using……;
相信大家看到using最多的地方应该是类似上面语句,换句话说,引用命名空间
然而,using最强大的功能并不在这里,看看以下的demo
// cs_using_statement.cs
// compile with /reference:System.Drawing.dll
using System.Drawing;
class a
...{
public static void Main()
...{
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial", 10.0f))
...{
// use MyFont and MyFont2
} // compiler will call Dispose on MyFont and MyFont2
Font MyFont3 = new Font("Arial", 10.0f);
using (MyFont3)
...{
// use MyFont3
} // compiler will call Dispose on MyFont3
}
}
<来自msdn>
// compile with /reference:System.Drawing.dll
using System.Drawing;
class a
...{
public static void Main()
...{
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial", 10.0f))
...{
// use MyFont and MyFont2
} // compiler will call Dispose on MyFont and MyFont2
Font MyFont3 = new Font("Arial", 10.0f);
using (MyFont3)
...{
// use MyFont3
} // compiler will call Dispose on MyFont3
}
}
<来自msdn>
关键的解释是这样的:
在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句。
实例化的对象必须实现 System.IDisposable 接口。
更通俗的理解是在using关键字后面的括号创建的非托管对像(必须实现IDisposable)在using大括号里面使用
之后会自动清理资源,在本地的msdn上面可以看到using 语句生成的il代码里面,实际是建构一个try..catch..
块,并在块里面调用Dispose,这样,我们以后可以常常在项目中为非托管资源加个外壳
using (init obj)
{
using obj
}
- 强大的using语句
- using 语句的用法
- using语句的用法
- [积累]using语句的作用
- c# using语句的用法
- C# using 语句的妙用
- C# using语句的作用
- C# using语句的使用
- 几个很强大的sql语句
- SQL语句的强大综合集锦
- python的with语句,超级强大
- python的with语句,超级强大
- using 语句
- using语句
- using 语句
- using语句
- using语句
- using语句
- Ajax实现分页查询
- 黑客基地
- 可视电话不是3G手机的杀手应用
- 试用EF开发WEB应用程序(3): Query String (附图)
- u-boot学习笔记二(脚本)
- 强大的using语句
- [转]Tomcat: Connector中HTTP与AJP区别
- tcl/tk参考——控制结构while
- blogonline
- 简单的xml处理类.
- Eclipse报告内存不足和PermSize space错误
- 深入探索Symbian活动对象开发
- [Perl][Mail]关于邮件的几个模块
- velocity学习笔记