using

根据微软MSDN上的解释,c#中的using共有三种用途:引用命名空间、为命名空间或类型创建别名、使用using语句。

1、引用命名空间

用using来引用命名空间,可以直接在程序中使用命名空间下的类型而不必指定详细的命名空间,如:using System.ServiceModel等等。

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

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

 
  
  1. //命名空间MyApplication中包含一个类MyClass  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5.  
  6. namespace MyApplication  
  7. {  
  8.     public class MyClass  
  9.     {  
  10.         public int Order = 1;  
  11.     }  
  12. }  
  13.  
  14.  
  15. //命名空间OtherApplication中也包含一个类MyClass  
  16. using System;  
  17. using System.Collections.Generic;  
  18. using System.Text;  
  19.  
  20. namespace OtherApplication  
  21. {  
  22.     public class MyClass  
  23.     {  
  24.         public int Number = 1;  
  25.     }  
  26. }  
  27.  
  28.  
  29. //在程序中使用using为两个相同名字的类型定义别名  
  30. using System;  
  31. using System.Collections.Generic;  
  32. using System.Text;  
  33. using ClassA = MyApplication.MyClass;  
  34. using ClassB = OtherApplication.MyClass;  
  35.  
  36. namespace UsingTest  
  37. {  
  38.     class Program  
  39.     {  
  40.         static void Main(string[] args)  
  41.         {  
  42.             ClassA ca = new ClassA();  
  43.             ca.Order = 1;  
  44.             ClassB cb = new ClassB();  
  45.             cb.Number = 2;  
  46.  
  47.             Console.WriteLine(ca.Order.ToString());  
  48.             Console.WriteLine(cb.Number.ToString());  
  49.         }  
  50.     }  

3、使用using语句,定义一个范围,在范围结束时清理对象。(注意:该对象必须实现了IDisposable接口)。其功能和try-->catch-->finally完全相同。如:

 
  
  1. string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";  
  2.  
  3. using (SqlConnection sqlCon = new SqlConnection(strCon))  
  4. {  
  5.     sqlCon.Open();  

注:这里SqlConnection默认实现了IDisposable接口,如果是自己写的类,那就要自己动手来实现IDisposable接口。

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

(1)对象必须实现IDisposable接口,否则编译器会报错误,如:

 
  
  1. //不能被编译  
  2. using (string strMsg="Test")  
  3. {  
  4.     Console.WriteLine(strMsg);  

(2)using对象检查是静态类型检查,并不支持运行时类型检查,因此如下错误也会出现编译错误:

 
  
  1. string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";  
  2. SqlConnection sqlCon = new SqlConnection(strCon);  
  3. object objCon = sqlCon;  
  4.  
  5. //不能被编译
  6. using (objCon)  
  7. {  
  8.     Console.WriteLine(objCon.ToString());  

但是可以通过“as”进行类型转换的方式来改进,如下:

 
  
  1. string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";  
  2. SqlConnection sqlCon = new SqlConnection(strCon);  
  3. object objCon = sqlCon;  
  4.  
  5. //可以被编译  
  6. using (objCon as IDisposable)  
  7. {  
  8.     Console.WriteLine(objCon.ToString());  

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

 
  
  1. string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";  
  2. string strCmd = "delete from t_Employee";  
  3.  
  4. using (SqlConnection sqlCon = new SqlConnection(strCon))  
  5. using (SqlCommand sqlCmd = new SqlCommand(strCmd, sqlCon))  
  6. {  
  7.     sqlCon.Open();  
  8.     sqlCmd.ExecuteNonQuery();  
  9.     sqlCon.Close();  

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

 
  
  1. using (Font MyFont1 =new Font("Arial",10.0f),MyFont2 = new Font("Arial",10.0f))  
  2. {  
  3.     //do something  

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

 

new

new的几种用法有:new运算符(用于创建对象和调用构造函数)、new修饰符(用于隐藏基类成员的继承成员)、new约束(用于在泛型声明中约束可能用作类型参数的参数的类型)等。

 1、new运算符

(1)用于创建对象和调用构造函数,如:

MyClass classA=new MyClass();

(2)用于为值类型调用默认的构造函数,如:

int myInt=new int();

myInt初始化为0,它是int类型的默认值。该语句的效果等同于:int myInt=0;

(3)、不能重载new运算符。

(4)、如果new运算符分配内存失败,则它将引发OutOfMemoryException异常。

2、new修饰符

使用new修饰符可以显式隐藏从基类继承的成员。若要隐藏继承的成员,请使用相同名称在派生类中声明该成员。