Effective C # principle 13: Initializing static members of a class with a static constructor

Source: Internet
Author: User
Tags constructor

Initializer in the above to "initialize", it is not good to hear, this article is translated into: "Preset method")

You should know that before any instance of a type initializes, you should initialize its static member variable. In C # You can use static preset methods and static constructors to achieve this goal. The static constructor of a class is unique and is executed before all methods, variables, or properties are accessed. You can use this function to initialize static member variables, enforce a single piece pattern, or implement any other work that should be done before an instance of the type is available. You can't initialize a variable with any instance constructors, other special private functions, or any other custom method (the compiler won't let you do this, so you don't have to worry about that).

As with the Out-of-the-way method of the instance, you can make the static preset method an alternative to the static constructor. If you need to simply assign a static member, use the initialization syntax directly. When you have more complex logic to initialize a static member variable, you create a static constructor:

public class MySingleton
{
  private static readonly MySingleton _theOneAndOnly =
  new MySingleton( );
 public static MySingleton TheOnly
 {
  get
  {
   return _theOneAndOnly;
  }
 }
 private MySingleton( )
 {
 }
 // remainder elided
}

You can simply implement a single piece pattern in the following ways, and you can actually have more complex logic when initializing a single piece pattern:

public class MySingleton
{
 private static readonly MySingleton _theOneAndOnly;
 static MySingleton( )
 {
  _theOneAndOnly = new MySingleton( );
 }
  public static MySingleton TheOnly
 {
  get
  {
   return _theOneAndOnly;
  }
 }
 private MySingleton( )
 {
 }
 // remainder elided
}

Similarly, as with the Out-of-the-way method of an instance, a static preset method executes before a static constructor call. Also, your static preset method is executed before the static constructor of the base class is executed.

When an application loads your data type for the first time, the CLR automatically calls the static constructor. You can only define a static constructor, and you cannot have parameters. Because static constructors are called by the CLR, you must pay attention to the generation of exceptions. If an exception is generated in a static constructor, the CLR will terminate your application directly. Because of exceptions, static constructors often replace static preset methods. If you use a static preset method, you cannot catch the exception yourself. As a static construct, you can do this (see Principle 45):

static MySingleton( )
{
 try {
  _theOneAndOnly = new MySingleton( );
 } catch
  {
  // Attempt recovery here.
 }
}

Static preset methods and static constructors provide the most refreshing way for your class to initialize static members. Unlike other languages, they are added to the C # language and are two different special locations for initializing static members.

Back to the Tutorial directory

Related Article

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.