Using, use

Source: Internet
Author: User

Using, use

The using keyword has two main purposes:

  • As an instruction, create an alias for the namespace or import the types in other namespaces.
  • Define a range as a statement. objects are released at the end of this range.
Using command

The using command has two purposes:

  • Import types in other namespaces:
  • using System.Text;
  • Create an alias for a namespace or type:
  • using Project = PC.MyCompany.Project;

Features of the using command:

  • The using command is restricted to files containing it. The using command in other files is invalid for existing files.
  • Using alias, used to easily limit the identifier to a namespace or type. The right side of the using alias command must be a fully qualified type. It has nothing to do with the preceding using command.
  • The using command is easy to use in the namespace, And the type does not need to be specified.
  • The using command does not provide access to any namespaces nested in the specified namespace.
Example 1

The following example shows how to define and use using aliases for namespaces.

namespace PC{    // Define an alias for the nested namespace.    using Project = PC.MyCompany.Project;    class A    {        void M()        {            // Use the alias            Project.MyClass mc = new Project.MyClass();        }    }    namespace MyCompany    {        namespace Project        {            public class MyClass { }        }    }}
The right side of the using alias command cannot be an open generic type. For example, you can create an alias for List. Example 2

The following shows how to define the using command and using alias for the class:

using System;// Using alias directive for a class.using AliasToMyClass = NameSpace1.MyClass;// Using alias directive for a generic class.using UsingAlias = NameSpace2.MyClass<int>;namespace NameSpace1{    public class MyClass    {        public override string ToString()        {            return "You are in NameSpace1.MyClass.";        }    }}namespace NameSpace2{    class MyClass<T>    {        public override string ToString()        {            return "You are in NameSpace2.MyClass.";        }    }}namespace NameSpace3{    // Using directive:    using NameSpace1;    // Using directive:    using NameSpace2;    class MainClass    {        static void Main()        {            AliasToMyClass instance1 = new AliasToMyClass();            Console.WriteLine(instance1);            UsingAlias instance2 = new UsingAlias();            Console.WriteLine(instance2);        }    }}// Output: //    You are in NameSpace1.MyClass.//    You are in NameSpace2.MyClass.
Using statement

Make it easy to use the IDisposable object correctly.

Example

The following shows how to use the using statement.

using (Font font1 = new Font("Arial", 10.0f)) {    byte charset = font1.GdiCharSet;}

File and Font are examples of hosting types used to access unmanaged resources. There are many other types of unmanaged resources and class libraries that encapsulate these resources. All these types must implement the IDisposable interface.

When using an IDisposable object, declare and instantiate this object in the using statement. The using statement can automatically call the Dispose method of an object and leave its scope when calling Dispose. In the using block, the object is read-only and cannot be modified or re-assigned.

The using statement ensures that the Dispose method is called even if an exception occurs. You can also put the object in the try block and call Dispose in the finally block to achieve the same effect. In fact, the compiler converts the using statement into this form. The above code will be extended to the following form during compilation:

{  Font font1 = new Font("Arial", 10.0f);  try  {    byte charset = font1.GdiCharSet;  }  finally  {    if (font1 != null)      ((IDisposable)font1).Dispose();  }}

As shown below, you can declare multiple instances of the type in the using statement.

using (Font font3 = new Font("Arial", 10.0f),            font4 = new Font("Arial", 10.0f)){    // Use font3 and font4.}

You can also pass the variables to the using statement after instantiating the resource object. However, this is not the best practice. In this case, the object is visible after the control leaves the using statement, but it may no longer have access to unmanaged resources. If you try to use this object outside the using block, an exception may occur. Therefore, it is best to instantiate this object in the using statement and limit its scope to the using block.

Font font2 = new Font("Arial", 10.0f);using (font2) // not recommended{    // use font2}// font2 is still in scope// but the method call throws an exceptionfloat f = font2.GetHeight();

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.