Talking about three usage of using from an instance in petshop

Source: Internet
Author: User
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!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.