1.using指令。using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到。
例如:using System;
using System.Data;
2.using别名。using + 别名 = 包括详细命名空间信息的具体的类型。
这种做法有个好处就是当同一个cs引用了两个不同的命名空间,但两个命名空间都包括了一个相同名字的类型的时候。当需要用到这个类型的时候,就每个地方都要用详细命名空间的办法来区分这些相同名字的类型。而用别名的方法会更简洁,用到哪个类就给哪个类做别名声明就可以了。注意:并不是说两个名字重复,给其中一个用了别名,另外一个就不需要用别名了,如果两个都要使用,则两个都需要用using来定义别名的。
例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using aClass = ConsoleApplication1.MyClass;
using bClass = ConsoleApplication2.MyClass;
namespace ConsoleApplication1
{
public class MyClass
{
public override string ToString()
{
return "You are in ConsoleApplication1.MyClass";
}
}
class TestUsing
{
}
}
namespace ConsoleApplication2
{
class MyClass
{
public override string ToString()
{
return "You are in ConsoleApplication2.MyClass";
}
}
}
namespace TestUsing
{
using ConsoleApplication1;
using ConsoleApplication2;
class ClassTestUsing
{
static void Main()
{
aClass my1 = new aClass();
Console.WriteLine(my1);
bClass my2 = new bClass();
Console.WriteLine(my2);
Console.WriteLine("Press any key");
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using aClass = ConsoleApplication1.MyClass;
using bClass = ConsoleApplication2.MyClass;
namespace ConsoleApplication1
{
public class MyClass
{
public override string ToString()
{
return "You are in ConsoleApplication1.MyClass";
}
}
class TestUsing
{
}
}
namespace ConsoleApplication2
{
class MyClass
{
public override string ToString()
{
return "You are in ConsoleApplication2.MyClass";
}
}
}
namespace TestUsing
{
using ConsoleApplication1;
using ConsoleApplication2;
class ClassTestUsing
{
static void Main()
{
aClass my1 = new aClass();
Console.WriteLine(my1);
bClass my2 = new bClass();
Console.WriteLine(my2);
Console.WriteLine("Press any key");
Console.Read();
}
}
}
3.using语句,定义一个范围,在范围结束时处理对象。
场景:
当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。
要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。
例如:
public static DataTable GetTable(string sql, int executeTimeOut, string connStringName)
{
DataTable dtRet = new DataTable();
using (SqlConnection sc = new SqlConnection(connStringName))
{
using (SqlDataAdapter sqa = new SqlDataAdapter(sql, sc))
{
sqa.SelectCommand.CommandTimeout = executeTimeOut;
sqa.Fill(dtRet);
return dtRet;
}
}
}
{
DataTable dtRet = new DataTable();
using (SqlConnection sc = new SqlConnection(connStringName))
{
using (SqlDataAdapter sqa = new SqlDataAdapter(sql, sc))
{
sqa.SelectCommand.CommandTimeout = executeTimeOut;
sqa.Fill(dtRet);
return dtRet;
}
}
}
利用iddasm看下编译后的代码:
.method public hidebysig static class [System.Data]System.Data.DataTable
GetTable(string sql,
int32 executeTimeOut,
string connStringName) cil managed
{
// Code size 88 (0x58)
.maxstack 3
.locals init ([0] class [System.Data]System.Data.DataTable dtRet,
[1] class [System.Data]System.Data.SqlClient.SqlConnection sc,
[2] class [System.Data]System.Data.SqlClient.SqlDataAdapter sqa,
[3] class [System.Data]System.Data.DataTable CS$1$0000,
[4] bool CS$4$0001)
IL_0000: nop
IL_0001: newobj instance void [System.Data]System.Data.DataTable::.ctor()
IL_0006: stloc.0
IL_0007: ldarg.2
IL_0008: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
IL_000d: stloc.1
.try
{
IL_000e: nop
IL_000f: ldarg.0
IL_0010: ldloc.1
IL_0011: newobj instance void [System.Data]System.Data.SqlClient.SqlDataAdapter::.ctor(string,
class [System.Data]System.Data.SqlClient.SqlConnection)
IL_0016: stloc.2
.try
{
IL_0017: nop
IL_0018: ldloc.2
IL_0019: callvirt instance class [System.Data]System.Data.SqlClient.SqlCommand [System.Data]System.Data.SqlClient.SqlDataAdapter::get_SelectCommand()
IL_001e: ldarg.1
IL_001f: callvirt instance void [System.Data]System.Data.Common.DbCommand::set_CommandTimeout(int32)
IL_0024: nop
IL_0025: ldloc.2
IL_0026: ldloc.0
IL_0027: callvirt instance int32 [System.Data]System.Data.Common.DbDataAdapter::Fill(class [System.Data]System.Data.DataTable)
IL_002c: pop
IL_002d: ldloc.0
IL_002e: stloc.3
IL_002f: leave.s IL_0055
} // end .try
finally
{
IL_0031: ldloc.2
IL_0032: ldnull
IL_0033: ceq
IL_0035: stloc.s CS$4$0001
IL_0037: ldloc.s CS$4$0001
IL_0039: brtrue.s IL_0042
IL_003b: ldloc.2
IL_003c: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0041: nop
IL_0042: endfinally
} // end handler
} // end .try
finally
{
IL_0043: ldloc.1
IL_0044: ldnull
IL_0045: ceq
IL_0047: stloc.s CS$4$0001
IL_0049: ldloc.s CS$4$0001
IL_004b: brtrue.s IL_0054
IL_004d: ldloc.1
IL_004e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0053: nop
IL_0054: endfinally
} // end handler
IL_0055: nop
IL_0056: ldloc.3
IL_0057: ret
} // end of method ClassTestUsing::GetTable
GetTable(string sql,
int32 executeTimeOut,
string connStringName) cil managed
{
// Code size 88 (0x58)
.maxstack 3
.locals init ([0] class [System.Data]System.Data.DataTable dtRet,
[1] class [System.Data]System.Data.SqlClient.SqlConnection sc,
[2] class [System.Data]System.Data.SqlClient.SqlDataAdapter sqa,
[3] class [System.Data]System.Data.DataTable CS$1$0000,
[4] bool CS$4$0001)
IL_0000: nop
IL_0001: newobj instance void [System.Data]System.Data.DataTable::.ctor()
IL_0006: stloc.0
IL_0007: ldarg.2
IL_0008: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
IL_000d: stloc.1
.try
{
IL_000e: nop
IL_000f: ldarg.0
IL_0010: ldloc.1
IL_0011: newobj instance void [System.Data]System.Data.SqlClient.SqlDataAdapter::.ctor(string,
class [System.Data]System.Data.SqlClient.SqlConnection)
IL_0016: stloc.2
.try
{
IL_0017: nop
IL_0018: ldloc.2
IL_0019: callvirt instance class [System.Data]System.Data.SqlClient.SqlCommand [System.Data]System.Data.SqlClient.SqlDataAdapter::get_SelectCommand()
IL_001e: ldarg.1
IL_001f: callvirt instance void [System.Data]System.Data.Common.DbCommand::set_CommandTimeout(int32)
IL_0024: nop
IL_0025: ldloc.2
IL_0026: ldloc.0
IL_0027: callvirt instance int32 [System.Data]System.Data.Common.DbDataAdapter::Fill(class [System.Data]System.Data.DataTable)
IL_002c: pop
IL_002d: ldloc.0
IL_002e: stloc.3
IL_002f: leave.s IL_0055
} // end .try
finally
{
IL_0031: ldloc.2
IL_0032: ldnull
IL_0033: ceq
IL_0035: stloc.s CS$4$0001
IL_0037: ldloc.s CS$4$0001
IL_0039: brtrue.s IL_0042
IL_003b: ldloc.2
IL_003c: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0041: nop
IL_0042: endfinally
} // end handler
} // end .try
finally
{
IL_0043: ldloc.1
IL_0044: ldnull
IL_0045: ceq
IL_0047: stloc.s CS$4$0001
IL_0049: ldloc.s CS$4$0001
IL_004b: brtrue.s IL_0054
IL_004d: ldloc.1
IL_004e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0053: nop
IL_0054: endfinally
} // end handler
IL_0055: nop
IL_0056: ldloc.3
IL_0057: ret
} // end of method ClassTestUsing::GetTable
由上可见,在语句块结束后,程序自动执行了Dispolse()。其实这种写法是try...finally的简化语法而已。