转C#中using和new的用法

C#中using和new的用法

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

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

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

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

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

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

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

(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. using (objCon)
  6. {
  7. Console.WriteLine(objCon.ToString());
  8. }

但是可以通过“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. using (objCon as IDisposable)
  6. {
  7. Console.WriteLine(objCon.ToString());
  8. }

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

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

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

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

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

new

(1)new  运算符  用于创建对象和调用构造函数。 (2)new  修饰符  用于隐藏基类成员的继承成员。 (3)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 修饰符显式隐藏从基类继承的成员。若要隐藏继承的成员,请使用相同名称在派生类中声明该成员,并用 new 修饰符修饰它。

请看下面的类:

1 publicclass MyClass 2 3 { 4 5    publicint x; 6 7    publicvoid Invoke() {} 8 9 } 10

在派生类中用 Invoke 名称声明成员会隐藏基类中的 Invoke 方法,即:

1 publicclass MyDerivedC : MyClass 2 3 { 4 5    newpublicvoid Invoke() {} 6 7 } 8

但是,因为字段 x 不是通过类似名隐藏的,所以不会影响该字段。

通过继承隐藏名称采用下列形式之一: 1.引入类或结构中的常数、指定、属性或类型隐藏具有相同名称的所有基类成员。 2.引入类或结构中的方法隐藏基类中具有相同名称的属性、字段和类型。同时也隐藏具有相同签名的所有基类方法。 3.引入类或结构中的索引器将隐藏具有相同名称的所有基类索引器。 4.在同一成员上同时使用 new override 是错误的。

注意:在不隐藏继承成员的声明中使用 new 修饰符将生成警告。

示例

在该例中,基类 MyBaseC 和派生类 MyDerivedC 使用相同的字段名 x,从而隐藏了继承字段的值。该例说明了 new 修饰符的使用。同时也说明了如何使用完全限定名访问基类的隐藏成员。

1 using System; 2 3 publicclass MyBaseC 4 5 { 6 7    publicstaticint x =55; 8 9    publicstaticint y =22; 10 11 } 12 13 14 15 publicclass MyDerivedC : MyBaseC 16 17 { 18 19    newpublicstaticint x =100;   // Name hiding20 21    publicstaticvoid Main() 22 23    { 24 25       // Display the overlapping value of x:26 27       Console.WriteLine(x); 28 29 30 31       // Access the hidden value of x:32 33       Console.WriteLine(MyBaseC.x); 34 35 36 37       // Display the unhidden member y:38 39       Console.WriteLine(y); 40 41    } 42 43 } 44

输出

100

55

22

如果移除 new 修饰符,程序将继续编译和运行,但您会收到以下警告:

The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'.

如果嵌套类型正在隐藏另一种类型,如下例所示,也可以使用 new 修饰符修改此嵌套类型。

示例

在该例中,嵌套类 MyClass 隐藏了基类中具有相同名称的类。该例不仅说明了如何使用完全限定名访问隐藏类成员,同时也说明了如何使用 new 修饰符消除警告消息。

1 using System; 2 3 publicclass MyBaseC 4 5 { 6 7    publicclass MyClass 8 9    { 10 11       publicint x =200; 12 13       publicint y; 14 15    } 16 17 } 18 19 20 21 publicclass MyDerivedC : MyBaseC 22 23 { 24 25    newpublicclass MyClass   // nested type hiding the base type members26 27    { 28 29      publicint x =100; 30 31       publicint y; 32 33       publicint z; 34 35    } 36 37 38 39    publicstaticvoid Main() 40 41    { 42 43       // Creating object from the overlapping class:44 45       MyClass S1 =new MyClass(); 46 47 48 49       // Creating object from the hidden class:50 51       MyBaseC.MyClass S2 =new MyBaseC.MyClass(); 52 53 54 55       Console.WriteLine(S1.x); 56 57       Console.WriteLine(S2.x);  58 59    } 60 61 } 62

输出

100

200

原文地址:https://www.cnblogs.com/baiyu/p/2815893.html