Usage of using

Source: Internet
Author: User

1.using instructions. Using + namespace name. The namespace name can be either a system or a class of its own definition.

2.using aliases. Using + aliases = Specific types that include detailed namespace information.

Conditions: When the same. cs file references two different namespaces, but two namespaces include a type of the same name

Format:

usingSystem;

usingaClass = NameSpace1.MyClass;

using bClass = NameSpace2.MyClass;Using namespace1{//define class}using namespace2{//define Class}3.using statement, define a scope and process the object at the end of the range. When an instance of a class is used in a code snippet, it is hoped that, for whatever reason, the dispose of the class instance is automatically invoked to release the resource, as long as it leaves the code snippet.
To achieve this goal, it is also possible to use try...catch to catch anomalies, but it is also convenient to use using. Example: Click on a button in the 1 Windows Form to draw a hint
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceprogram{ Public Partial classForm_main:form { PublicForm_main () {InitializeComponent (); }Private voidBtn_click (Objectsender, EventArgs e) {using(NewTest ())//to create a test object in a using statement{}//the Dispose method of the test object is automatically called after execution of the using statement block is completed}classTest:idisposable//defining the test class to implement the IDisposable interface{ Public voidDispose ()//Implementing interface Methods{MessageBox.Show ("the Dispose method of the test object has been executed","Tips");//Pop-up message dialog box} }}}

2 • Console Application

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespaceuseusing{classProgram {Static voidMain (string[] args) {using(Myusing mu =Newmyusing ())    {} console.readline (); }}classmyusing:idisposable { Public voidDispose ()//Implementing interface Methods      {Console.WriteLine ("Dispose is called"); }}}

Usage of using

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.