文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > C#中using关键字
侵权投诉

C#中using关键字

dd


// Release unmanaged resources

m_disposed = true;

}

}

/**//// <summary>

/// 析构函数

/// </summary>

~Employee()

{

Dispose(false);

}

private bool m_disposed;

#endregion

}

使用using语句需要注意的几点:

3.1、对象必须实现IDisposeable接口,这个已经说过,如果没有实现编译器会报错误。

如:

using( string strMsg = "My Test" )

{

Debug.WriteLine( strMsg );//Can't be compiled

}

3.2、第二个using对象检查是静态类型检查,并不支持运行时类型检查,因此如下形式也会出现编译错误。

SqlConnection sqlConn = new SqlConnection( yourConnectionString );

object objConn = sqlConn;

using ( objConn )

{

Debug.WriteLine( objConn.ToString() );//Can't be compiled

}

不过对于后者,可以通过“as”来进行类型转换方式来改进。

SqlConnection sqlConn = new SqlConnection( yourConnectionString );

object objConn = sqlConn;

using ( objConn as IDisposable )

{

Debug.WriteLine( objConn.ToString() );

}

3.3、当同时需要释放多个资源时候,并且对象类型不同,可以这样写:

using( SqlConnection sqlConn = new SqlConnection( yourConnectionString ) )

using( SqlCommand sqlComm = new SqlCommand( yourQueryString, sqlConn ) )

{

sqlConn.Open();//Open connection

/Operate DB here using "sqlConn"

sqlConn.Close();//Close connection

}

如果对象类型相同,可以写到一起:

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

3.4、using关键字只是针对C#语句,对于VB等其他语言还没有对应的功能。

关于.Net资源释放问题,也就是对于非内存的资源,最好使用IDisposable接口提供的Dispose来释放。
很明显,Dispose方法是一个外部方法,系统并不会帮你调用。为了尽早释放对象所占用的资源,所以需要保证Dispose方法能尽早被执行。那么在.Net中提供了一个比较简便的方法,就是对于实现了IDisposable接口的类型对象提供了using语句。


就操作一个数据库这个例子来说,使用using语句可以如下:


using( SqlConnection sqlConn = new SqlConnection( yourConnectionString ) )

{

sqlConn.Open(); //Open connection

//Operate DB here using "sqlConn"

sqlConn.Close(

上一页第2页

你可能喜欢

热门文档

相关文档

  • C# 关键字--using

    关键字---using C# 关键字--using using 关键字有两个主要用途: 关键字有两...Power C-C++ Debugging-... 16页 免费 using GNU C as a porta... 15...

  • C# using关键字

    C# using关键字_计算机软件及应用_IT/计算机_专业资料。C#中using的使用在...Using Annotated C 暂无评价 7页 免费 喜欢此文档的还喜欢 C#串口通信代码 42...

  • .net C# 中 using的几种用法

    C#中using关键字用法 5页 免费.net C# 中 using的几种用法 ...

  • c#关键字

    Csharp关键字C#关键字完整列表 abstract as ...using value virtual volatile volatile void where while yield 其中有几个比较...

  • c#中成员关键字

    C#关键字 4页 免费c#中成员关键字 ...10 using引用命名空间关键字,namespace声名命名空间关键字,class声名类的关键字。...

  • C#中的关键字

    using 语句/指令:using 语句限定使用命名空间的范围,using 指令创建命名空 间的...C关键字 4页 免费 C 中的sizeof 关键字详... 3页 免费 ARM C嵌入式编程...

  • c#关键字大全

    c#关键字大全_IT/计算机_专业资料。花了一中午时间搜集了c#全部关键字解释及示例...using this: Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this)); } ...

  • C#中的关键字大全

    (泛型修饰 符) protected return sizeof struct True ulong using while long ...C语言关键字大全 3页 1下载券 C#中的关键字大全 暂无评价 6页 1下载券 ASCII...

  • C#关键字详解

    对象。 例子: using System; using System.Collections.Generic; using System.Text; namespace 真的是关键字 { class C : IDisposable //继承 IDisposable 接口 ...

  • c#关键字

    (类似于C 和C++语言中的函数指针) do 一种循环程序流程结构,导致语句或语句块...using 一个用于创建名称空间别名的关键字,对于名称空间中的数据类型,也可用于...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email