The following code is available in petshop4.0:
Code
1 Public ilist <categoryinfo> getcategories (){
2
3 ilist <categoryinfo> categories = new list <categoryinfo> ();
4
5 // execute a query to read the categories
6 using (sqldatareader RDR = sqlhelper. executereader (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL _select_categories, null )){
7 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}
Using is used in row 6th. Here I want to remind myself that this is one of the usage of using.
There are three using usage methods:
1. Reference namespace to reduce redundant code.
Using system. Web. UI. webcontrols;
2. Create a namespace alias (using alias ).
Using myalias = mycompany. proj. nested;
3. release resources instantly.
Using (testobject A = new testobject ())
{
// Use Object
}
// The object resource is released.
The above code is equivalent to: Code
1try {
2 sqldatareader RDR = sqlhelper. executereader (sqlhelper. connectionstringlocaltransaction, commandtype. Text, SQL _select_categories, null );
3 while (RDR. Read ())
4 {
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 {RDR. Dispose ()}
14
The requirement for this usage is the class in the using () brackets.Implement the idisposable InterfaceOtherwise, an error will occur during variation!