使用using 语句
2004-05-10 17:22
155 查看
在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句。
实例化的对象必须实现 System.IDisposable 接口。
示例
// 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
}
}
实例化的对象必须实现 System.IDisposable 接口。
示例
// 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
}
}
相关文章推荐
- 使用SqlConnection连接SQL server2008 R2数据库的语句using System.Data.SqlClient;using System.Data.SqlClient;
- 读代码随笔(一):using语句的使用
- 在使用EF开发时候,遇到 using 语句中使用的类型必须可隐式转换为“System.IDisposable“ 这个问题。
- EF_using 语句中使用的类型必须可隐式转换为“System.IDisposable“
- 使用using指令呵using语句
- 常识(5)- 使用动态SQL语句(Using dynamic SQL),SQLSA,SQLDA
- C# using语句使用心得总结
- C# using语句的使用
- using语句下使用SqlDataReader异常:Invalid attempt to Read when reader is closed
- using 语句中使用的类型必须可隐式转换为“System.IDisposable“
- C# using语句的使用
- using 语句使用注意点
- using 语句中使用的类型必须可隐式转换为“System.IDisposable
- C#中连接SqlServer数据库并且使用事务和using语句的实例
- 在using语句中使用return
- C# using语句使用流还需要显示调用Close方法吗?
- 在使用EF开发时候,遇到 using语句中使用的类型必须可隐式转换为System.IDisposE
- 教你50招提升ASP.NET性能(九):显式的使用using语句减少内存泄露
- 使用Using语句块高效地使用连接
- 使用merge-using语句初始化数据库