C # Proverbs: initializing static members with static constructors

Source: Internet
Author: User
Tags constructor exception handling

When writing programs, it is unavoidable to use static members, because static member access is detached from the type object, so the use of non-static constructors, private methods or some other methods are unreasonable. NET provides member initialization statements and static constructors to initialize static members.

Based on the previous unit, we can know that the initialization statements for static members are performed earlier than the static constructor, followed by the benefits and limitations of the member initialization statements. One thing to mention here is the difference between a static constructor and an instance constructor, because a static constructor is executed by a CLR call, so a static constructor can only be a single and cannot have parameters. Then there are some drawbacks to the static constructors in relation to the member initialization.

In addition, the biggest problem with member initialization is that the exception cannot be captured, and it may be possible for ordinary members to add Try-catch when constructing type objects, and for static members, it is sometimes not possible to determine which type of access statement is executed first. Adding Try-catch to each use type reduces the readability of the program. But if this anomaly is emitted, it can cause the entire program to crash. Then using the static constructor can better catch the exception, and do exception handling.

A comparison of static member initialization statements and static constructors is generally as follows.

Simplicity of Security Exception handling
Static member initialization Most convenient More secure Very difficult
Static constructors Relatively simple Most secure Easy

So what exactly is the way to initialize static members, give the following two points of advice:

The first is the simple static member, for example, the type is a value type, and so on, using the member initialization statement to complete;

If the second static member initialization is more complex, or if there is a possibility of an exception, it is done with a static constructor.

The content of this unit is basically finished, now say static member The most important one application "Singleton", is to guarantee this type of object uniqueness. In fact, the implementation is very simple, the following is a simple example.

public class MySingleton
{
 private static readonly MySingleton _theOneAndOnly;
 static MySingleton()
 {
  _theOneAndOnly = new MySingleton();
 }
 public static MySingleton TheOnly
 {
  get{ return _theOneAndOnly; }
 }
 /// <summary>
 /// Private constructor to avoid object created from outside
 /// </summary>
 private MySingleton()
 {}
}

When a static member is initialized, it is possible to look closely at the object construction steps that I mentioned in the previous unit. may seem abstract, especially when analyzing nested relationships, now give an example to illustrate.

class A
{
 public static int X = B.Y;
 static A()
 {
  ++X;
 }
}
class B
{
 public static int Y = A.X;
 static B()
 {
  ++Y;
 }
}

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.