查看完整版本: 深入C#学习系列之不可小瞧的using关键字

Teacher 2008-9-4 13:27

深入C#学习系列之不可小瞧的using关键字

using关键字微软MSDN上解释总共有三种用途:1、引用命名空间。2、为命名空间或类型创建别名。3、使用using语句。

  1、引用命名空间,这样就可以直接在程序中引用命名空间的类型而不必指定详细的命名空间。

  这个就不用说了吧,比如大家最常用的:using System.Text;

  2、为命名空间或类型创建别名:

  当同一个cs引用了不同的命名空间,但这些命名控件都包括了一个相同名字的类型的时候,可以使用using关键字来创建别名,这样会使代码更简洁。注意:并不是说两个名字重复,给其中一个用了别名,另外一个就不需要用别名了,如果两个都要使用,则两个都需要用using来定义别名的。

  using System;

  using aClass = NameSpace1.MyClass;

  using bClass = NameSpace2.MyClass;

  ……

  //使用方式

  aClass my1 = new aClass();

  Console.WriteLine(my1);

  bClass my2 = new bClass();

  Console.WriteLine(my2);

  3、使用using语句,定义一个范围,在范围结束时处理对象。(不过该对象必须实现了IDisposable接口)。其功能和try ,catch,Finally完全相同。

  比如:

  using (SqlConnection cn = new SqlConnection(SqlConnectionString)){……}//数据库连接

  using (SqlDataReader dr = db.GetDataReader(sql)){……}//DataReader

  PS:这里SqlConnection和SqlDataReader对象都默认实现了IDisposable接口,如果是自己写的类,那就要自己手动来实现IDisposable接口。比如:

  using (Employee emp = new Employee(userCode))

  {

  ……

  }

  Emlpoyee.cs类:

  public class Employee:IDisposable

  {

  实现IDisposable接口#region 实现IDisposable接口

  /**//// <summary>

  /// 通过实现IDisposable接口释放资源

  /// </summary>

  public void Dispose()

  {

  Dispose(true);

  GC.SuppressFinalize(this);

  }

Teacher 2008-9-4 13:28

/**//// <summary>

  /// 释放资源实现

  /// </summary>

  /// <param name="disposing"></param>

  protected virtual void Dispose(bool disposing)

  {

  if (!m_disposed)

  {

  if (disposing)

  { // Release managed resources

  if(db!=null)

  this.db.Dispose();

  if(dt!=null)

  this.dt.Dispose();

  this._CurrentPosition = null;

  this._Department = null;

  this._EmployeeCode = null;

  }

  // 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等其他语言还没有对应的功能。

sanky 2008-10-26 11:48

好多好东西要学呀,努力学习
页: [1]
查看完整版本: 深入C#学习系列之不可小瞧的using关键字