从petshop中一实例谈using 的三种用法
在petshop4.0中有这样一段代码:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public IList<CategoryInfo> GetCategories()
{
2![](/Images/OutliningIndicators/InBlock.gif)
3
IList<CategoryInfo> categories = new List<CategoryInfo>();
4![](/Images/OutliningIndicators/InBlock.gif)
5
//Execute a query to read the categories
6![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, null))
{
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while (rdr.Read())
{
8
CategoryInfo cat = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));
9
categories.Add(cat);
10
}
11
}
12
return categories;
13
}
在第6行处用到了using,这里我想提醒自己的是,这是using的一种用法之一。
using共有三种用法:
1.引用命名空间,减少冗余代码。
Using System.Web.UI.WebControls;
2.创建命名空间的别名(using 别名)。
using MyAlias = MyCompany.Proj.Nested;
3.即时释放资源。
Using (TestObject a = new TestObject())
{
// 使用对象
}
//对象资源被释放
上面代码其实等同于:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
try
{
2
SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text,SQL_SELECT_CATEGORIES, null) ;
3
while (rdr.Read())
4![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
CategoryInfo cat = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));
6
categories.Add(cat);
7
}
8
9
return categories;
10
11
}
12
finally
13![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{rdr .Dispose()}
14
这种用法的要求是在using()括号里面的类要实现IDisposable接口,否则的话变异的时候会出错!
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/dot.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/dot.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/dot.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
在第6行处用到了using,这里我想提醒自己的是,这是using的一种用法之一。
using共有三种用法:
1.引用命名空间,减少冗余代码。
Using System.Web.UI.WebControls;
2.创建命名空间的别名(using 别名)。
using MyAlias = MyCompany.Proj.Nested;
3.即时释放资源。
Using (TestObject a = new TestObject())
{
// 使用对象
}
//对象资源被释放
上面代码其实等同于:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/dot.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/dot.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/dot.gif)
14
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)