1.using指令。using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到。^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 例如:using System;^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå using System.Data;^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 2.using别名。using + 别名 = 包括详细命名空间信息的具体的类型。^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 这种做法有个好处就是当同一个cs引用了两个不同的命名空间,但两个命名空间都包括了一个相同名字的类型的时候。当需要用到这个类型的时候,就每个地方都要用详细命名空间的办法来区分这些相同名字的类型。而用别名的方法会更简洁,用到哪个类就给哪个类做别名声明就可以了。注意:并不是说两个名字重复,给其中一个用了别名,另外一个就不需要用别名了,如果两个都要使用,则两个都需要用using来定义别名的。^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 例如:^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå ^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using System;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using System.Collections.Generic;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using System.Linq;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using System.Text;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using aClass = ConsoleApplication1.MyClass;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using bClass = ConsoleApplication2.MyClass;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
namespace ConsoleApplication1
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
public
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
class MyClass
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
public
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
override
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
string ToString()
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
return "You are in ConsoleApplication1.MyClass";
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
class Test
Using
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
namespace ConsoleApplication2
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
class MyClass
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
public
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
override
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
string ToString()
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
return "You are in ConsoleApplication2.MyClass";
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
namespace TestUsing
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using ConsoleApplication1;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using ConsoleApplication2;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
class ClassTestUsing
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
static
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
void Main()
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
aClass my1 = new aClass();
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
Console.WriteLine(my1);
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
bClass my2 = new bClass();
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
Console.WriteLine(my2);
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
Console.WriteLine("ress any key");
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
Console.Read();
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå ^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 3.using语句,定义一个范围,在范围结束时处理对象。^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 场景:^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå 例如:^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå ^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
public
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
static DataTable GetTable(string sql, int executeTimeOut, string connStringName)
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
DataTable dtRet = new DataTable();
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using (SqlConnection sc = new SqlConnection(connStringName))
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
using (SqlDataAdapter sqa = new SqlDataAdapter(sql, sc))
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
{
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
sqa.SelectCommand.CommandTimeout = executeTimeOut;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
sqa.Fill(dtRet);
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
return dtRet;
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}
^ÊÆ#Y"bbs.51aspx.comiÛóáö°»õå
}