Using and new in C #

Source: Internet
Author: User

Using:

1. equivalent to the Java import function and introduced functions;

2. When clearing resources, use the using keyword for deterministic termination;
The time for using is when the object uses unmanaged resources or valuable resources,
For managed or non-precious resources, I can rely on GC, and there is no need to add myself. In addition, try blocks will affect efficiency.

The Using statement defines a range to process objects at the end of the range.
Scenario:
WhenCodeThe class instance is used in the section, and you want to automatically call the dispose of the class instance as long as you leave the code segment for whatever reason.
To achieve this goal, it is also possible to catch exceptions using try... catch, but it is also convenient to use using.
The object provided for the using statement must implement the idisposable interface. This interface provides the dispose method, which releases the resources of this object.

Essence of using statements
The IL code generated using the using statement is actually a try, finally code block, releasing resources in the finally code block.

3. Using alias. Using + alias = contains the specific type of detailed namespace information.
This method has the advantage that when the same CS references two different namespaces, but both namespaces contain the same name type. When this type is required, the detailed namespace method should be used in every place to distinguish the types with the same name. The alias method is more concise. You can declare the alias for the class you use.

New:

1. First, the new modifier and the new operator are two concepts. The new modifier can only be used to inherit classes and make up for the shortcomings of the base class design. The new operator is used to instantiate a type;

2. The new modifier and override are mutually exclusive in meaning listing, so they cannot act on the same member;

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.