You cannot underestimate the using keyword.

Source: Internet
Author: User
The Using Keyword. People who do not know it may not care about it. Isn't it used to reference a namespace? But if you really have a deep understanding of using, you will not underestimate it. Let me show you the usage and usage skills of using.
The Using Keyword Microsoft msdn has three purposes: 1. Reference namespace. 2. Create an alias for the namespace or type. 3. Use the using statement.
1. Reference a namespaceIn this way, you can directly reference the namespace type in the program without specifying a detailed namespace.
This is not necessary, for example, the most commonly used: using system. text;
2. Create an alias for the namespace or Type:
When the same CS references different namespaces, but these naming controls all include the same name type, you can use the using keyword to create aliases, which makes the code more concise. Note: This does not mean that the two names are repeated. If you use an alias for one of them, you do not need to use the alias for the other. If you want to use both of them, you need to use using to define the alias for both of them.
Using system;
Using Aclass = namespace1.myclass;
Using bclass = namespace2.myclass;
......
// Usage
Aclass my1 = new Aclass ();
Console. writeline (my1 );
Bclass my2 = new bclass ();
Console. writeline (my2 );
3. Use the using statement to define a range and process objects at the end of the range.(However, this object must implement the idisposable interface ). Its functions are exactly the same as try, catch, and finally.
For example:
Using (sqlconnection Cn = new sqlconnection (sqlconnectionstring) {...} // database connection
Using (sqldatareader DR = dB. getdatareader (SQL) {...} // datareader
PS: Both sqlconnection and sqldatareader objects implement the idisposable interface by default. If they are self-written classes, manually implement the idisposable interface. For example:
Using (employee EMP = new employee (usercode ))
{
......
}
Emlpoyee. CS class:

Public class employee: idisposable
{

Implement the idisposable interface # region implement the idisposable Interface
/** // <Summary>
/// Release resources by implementing the idisposable Interface
/// </Summary>
Public void dispose ()
{
Dispose (true );
GC. suppressfinalize (this );
}
/** // <Summary>
/// Release resource implementation
/// </Summary>
/// <Param name = "disposing"> </param>
Protected virtual void dispose (bool disposing)
{
If (! M_disposed)
{
If (disposing)
{
// Release managed resources
If (DB! = NULL)
This. DB. Dispose ();
If (DT! = NULL)
This. dt. Dispose ();
This. _ currentposition = NULL;
This. _ Department = NULL;
This. _ employeecode = NULL;

}
// Release unmanaged Resources
M_disposed = true;
}
}
/** // <Summary>
/// Destructor
/// </Summary>
~ Employee ()
{
Dispose (false );
}
Private bool m_disposed;

# Endregion
}


Note the following when using the using statement:
3.1. The object must implement the idisposeable interface. As mentioned above, if the compiler is not implemented, an error will be reported.
For example:

Using (string strmsg = "my test ")
{
Debug. writeline (strmsg); // can't be compiled

}
3.2. The second using object check is a static type check and does not support runtime type check. Therefore, compilation errors may occur as follows.
Sqlconnection sqlconn = new sqlconnection (yourconnectionstring );
Object objconn = sqlconn;
Using (objconn)
{
Debug. writeline (objconn. tostring (); // can't be compiled
}

However, for the latter, the type conversion method can be improved through ". Sqlconnection sqlconn = new sqlconnection (yourconnectionstring); object objconn = sqlconn; using (objconn as idisposable) {Debug. writeline (objconn. tostring ());}

3.3 When multiple resources need to be released at the same time and the object type is different, you can write as follows:
Using (sqlconnection sqlconn = new sqlconnection (yourconnectionstring ))

Using (sqlcommand sqlcomm = new sqlcommand (yourquerystring, sqlconn ))
{
Sqlconn. open (); // open connection // operate dB here using "sqlconn" sqlconn. Close (); // close connection
}
If the object types are the same, you can write them together:
Using (font myfont = new font ("Arial", 10.0f), myfont2 = new font ("Arial", 10.0f ))
{
// Use myfont and myfont2
} // Compiler will call dispose on myfont and myfont2
3.4. The using keyword is only for C # statements. Other languages such as VB have no corresponding functions.
PS: This article is a summary of personal learning. For some content, refer to relevant articles on the Internet. If you find that your personal summary has incorrect cognition or omissions, Please comment and share your comments.

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.