using
根据微软MSDN上的解释,c#中的using共有三种用途:引用命名空间、为命名空间或类型创建别名、使用using语句。
1、引用命名空间
用using来引用命名空间,可以直接在程序中使用命名空间下的类型而不必指定详细的命名空间,如:using System.ServiceModel等等。
2、为命名空间或类型创建别名
当一个类引用了不同的命名空间,但这些命名空间都包括了一个相同名字的类型时,可以使用using关键字来创建别名,这样会使代码更简洁。注意:并不是说两个类型名字重复,给其中一个使用了别名,另外一个就不需要用别名了,如果两个类型都要在该类中使用,则两个都需要用using来定义别名,如下:
- //命名空间MyApplication中包含一个类MyClass
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace MyApplication
- {
- public class MyClass
- {
- public int Order = 1;
- }
- }
- //命名空间OtherApplication中也包含一个类MyClass
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace OtherApplication
- {
- public class MyClass
- {
- public int Number = 1;
- }
- }
- //在程序中使用using为两个相同名字的类型定义别名
- using System;
- using System.Collections.Generic;
- using System.Text;
- using ClassA = MyApplication.MyClass;
- using ClassB = OtherApplication.MyClass;
- namespace UsingTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- ClassA ca = new ClassA();
- ca.Order = 1;
- ClassB cb = new ClassB();
- cb.Number = 2;
- Console.WriteLine(ca.Order.ToString());
- Console.WriteLine(cb.Number.ToString());
- }
- }
- }
3、使用using语句,定义一个范围,在范围结束时清理对象。(注意:该对象必须实现了IDisposable接口)。其功能和try-->catch-->finally完全相同。如:
- string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";
- using (SqlConnection sqlCon = new SqlConnection(strCon))
- {
- sqlCon.Open();
- }
注:这里SqlConnection默认实现了IDisposable接口,如果是自己写的类,那就要自己动手来实现IDisposable接口。
使用using语句需要注意的几点:
(1)对象必须实现IDisposable接口,否则编译器会报错误,如:
- //不能被编译
- using (string strMsg="Test")
- {
- Console.WriteLine(strMsg);
- }
(2)using对象检查是静态类型检查,并不支持运行时类型检查,因此如下错误也会出现编译错误:
- string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";
- SqlConnection sqlCon = new SqlConnection(strCon);
- object objCon = sqlCon;
- //不能被编译
- using (objCon)
- {
- Console.WriteLine(objCon.ToString());
- }
但是可以通过“as”进行类型转换的方式来改进,如下:
- string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";
- SqlConnection sqlCon = new SqlConnection(strCon);
- object objCon = sqlCon;
- //可以被编译
- using (objCon as IDisposable)
- {
- Console.WriteLine(objCon.ToString());
- }
(3)当同时需要释放多个资源时,并且对象类型不同,可以这样写:
- string strCon = "Data Source=.;Initial Catalog=WCFPT;uid=sa;pwd=sa";
- string strCmd = "delete from t_Employee";
- using (SqlConnection sqlCon = new SqlConnection(strCon))
- using (SqlCommand sqlCmd = new SqlCommand(strCmd, sqlCon))
- {
- sqlCon.Open();
- sqlCmd.ExecuteNonQuery();
- sqlCon.Close();
- }
如果对象类型相同,可以写到一起:
- using (Font MyFont1 =new Font("Arial",10.0f),MyFont2 = new Font("Arial",10.0f))
- {
- //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修饰符可以显式隐藏从基类继承的成员。若要隐藏继承的成员,请使用相同名称在派生类中声明该成员。
转载于:https://blog.51cto.com/fuyang/481921