Using usage in C #

Source: Internet
Author: User

Using usage in C #

I wrote an example about how to use wcf. There is a using () {} sentence in it. In the past, using not only references namespaces, but also has other functions.

 

The using keyword is used in C # in three ways:

1. Used in namespaces such as using System. Everyone knows this. In order not to write the full name of the class (you can write System. Console as the Console)
2. rename a class

 

Rename a class for ease of use, such:
Using System;
Using hahaha = System. Console;
Namespace Console001
{
Public class Class1
{
Public static void Main ()
{
Hahaha. WriteLine (second usage of using !);
}
}
}

3. Resource Management statement Function

 

In addition to using System, using Dos = System. Console, using in C # also provides the resource management statement function:
Using (R r1 = new R ()){
R1.F ();
}
In C #, it is translated:
R r1 = new R ();
Try {
R1.F ();
}
Finally {
If (r1! = Null) (IDisposable) r1). Dispose ();
}

Of course, r1 must support the Dispose () method.


 

Related Article

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.