Objective C # excerpt (2)-. NET Resource Management

Source: Internet
Author: User

12: The Initialization is better than the value assignment statement during variable declaration.
Prefer Variable Initializers to Assignment Statements.
If the instance variable member of the class is initialized upon declaration, the initialization code will be inline to each instance constructor of the class: when constructing an object instance, the initialization code will be executed first, then, call the base class constructor and execute the code in the constructor.
When the constructor has multiple overloaded versions, we recommend that you avoid initialization when defining fields. Instead, you should place these public initialization statements in a constructor, then, other constructors call the initialization constructor explicitly.

13: Use the static constructor to initialize static class members.
Initialize Static Class Members with Static Constructors.
If the static variable member in the class is initialized during declaration, the initialization code will be inline to the static constructor of the class: when running, the initialization code will be executed first, and then the code in the static constructor will be executed. Suggestion: simple static members can be initialized directly during declaration, and initialization that may cause exceptions should be put in static constructor initialization.

14: Use the constructor chain.
Utilize Constructor Chaining.
You can only use this (args) (Base called in the last constructor In the constructor chain) and base (args), and cannot use them at the same time.

15: Use using and try/finally statements to clear resources.
Utilize using and Try/finally for Resource Cleanup.
The objects used by using must inherit IDisposable and do not support runtime type check (as conversion is available, but bugs may be hidden, generally, make sure that each object implementing the IDispose interface is allocated within the using range ).

16: Minimize memory spam.
Minimize Garbage.
Use class members instead of the local variables used in each function. Use the Singleton mode to implement a public object or implement it as an object pool.

17: Try to reduce packing and unpacking.
Minimize Boxing and Unboxing.
Avoid implicit packing. Eg: Console. writeLine ("Number list: {0}, {1}, {2}", 1, 2, 3); => Console. writeLine ("Number list: {0}, {1}, {2}", 1. toString (), 2. toString (), 3. toString ());

18: implement the standard Dispose mode.
Implement the Standard Dispose Pattern.
What should be done by Dispose (): release all unmanaged resources; release all managed resources, including canceling event binding; set a flag to indicate that the object has been released; call "GC. suppressFinalize (this) "to avoid GC and then remove the object's destructor.
If both the parent class and Child class use unmanaged resources, the Child class must explicitly call the Dispose () of the parent class in the Dispose () function of the heavy load parent class ().

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.