Using keyword Usage summary in C #

Source: Internet
Author: User
Tags finally block

The Using keyword in C # has two main uses:

1. Using as directive:

(1) used to introduce namespaces into the current unit, for example: using System.Windows.Forms;

(2) Create an alias for the existing namespace, for example: using Form = System.Windows.Forms;

2. Using as statement: Defines a code block scope that frees resources at the end of the code block.

(1) Declare an object before the using statement:

1 New Class (); 2 using (C1) 3 {4     // Use C1 5 }

(2) Declaring an object in a using statement, which must be declared inside a using statement when more than one object is to be used:

1 using New New Class ()) 2 {3     // Use c1, C2 4 }

During the program compilation phase, the compiler automatically builds the using statement into a try-catch-finally statement and calls the object's Dispose method in the finally block to clean up the resource.

Using keyword Usage summary in C #

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.