Use public static attributes instead of public static fields (C #)

Source: Internet
Author: User

To improveCodeWe often use static object methods. However, when using static objects, especially a public class, the class name is testclass:

1,ProgramMember A needs a static object, so a code public staticobjecta A = new staticobjecta () is added to the public class ();

2. programmer B needs a static object, so a code public staticobjectb B = new staticobjectb () is added to the public class ();

....

N. programmer n needs a static object, so a code public staticobjectn n = new staticobjectn is added to the public class );

1 Public Testclass
2 {
3 Public Static Staticobjecta =   New Staticobjecta ();
4 Public Static Staticobjectb B =   New Staticobjectb ();
5 // .....
6 Public Static Staticobjectn n =   New Staticobjectn ();
7 }

It seems that everyone gets what they want. In fact, after testclass is compiled, the code will be changed to the following:

Public Testclass
{
Static Staticobjecta;
Static Staticobjectb B;
// .....
Static Staticobjectn N;

Static Testclass ()
{
A =   New Staticobjecta ();
B =   New Staticobjectb ();
// .....
C =   New Staticobjectn ();
}
}

After the code is compiled, all the static object initialization is moved to the static constructor, because the static object is initialized when the object is used for the first time, that is, when an object is called for the first time, it will first execute the static constructor of the object (unless there is no static object in the class ).

Imagine what will happen if the object staticobjecta needs to be called during the initialization process of the object staticobjectb? If you do not analyze it carefully, you will surely think this problem is incredible, because static objects do not need to be initialized. Why does it still report"The instance where the object reference is not set to the object"This is an error.

In fact, this error is caused by the initialization sequence of the static object. When the object staticobjectb is used, the object is not initialized yet. If this public class is used by several people, it is very likely that this problem will occur. How can this problem be avoided?

Here is a method (of course, if you are familiar with the initialization of each object and adjust the order of each object, you don't need the method proposed here): static attributes. Paste the Code directly here.

Public Testclass
{
STaticStaticobjecta _; PublicStaticStaticobjecta { If (_ A = NULL) { _ A =NewStaticobjecta ();
} Return _; }
STaticStaticobjectb _ B; PublicStaticStaticobjectb B { If (_ B = NULL) { _ B =NewStaticobjectb ();
} Return _ B; }
// ..... S Tatic Staticobjectn _ N;

PublicStaticStaticobjectn n { If (_ n = NULL) { _ N =NewStaticobjectn ();
} Return _ N; }
}

The advantages of using static attributes are as follows::

1. Initialization of objects inside the class is delayed. That is to say, when the testclass object is called for the first time, it does not need to execute the static constructor of the object.

2. The first time an object is called, it is faster. This advantage is derived from 1. Because no static constructor needs to be called, initialization of all static objects is omitted. Of course, the first call will be faster.

3. I do not know yet. Please submit it!

The disadvantages of using static attributes are as follows::

1. Each static object must be defined twice. One is public and the other is private.

2. I do not know yet. Please submit it!

Based on the above content, I suggest using static attributes. I don't know what you think above. Thank you for your advice!

 

The following is a digress

In fact, no matter whether it is a non-static property object or a static property object, there will be two references to the same property object in the memory.

For example, we have a class:

Public   Class Testclass
{
Public   String Stringvalue1 { Get ; Set ;}
Public   Static   String Stringvalue2 { Get ; Set ;}
}

It seems that the classes with two lines of code will actually become the following code after being compiled:

Public   Class Testclass
{
// Fields
[Compilergenerated]
Private   String   < Stringvalue1 > K _ backingfield;
[Compilergenerated]
Private   Static   String   < Stringvalue2 > K _ backingfield;

// Properties
Public   String Stringvalue1
{
[Compilergenerated]
Get
{
Return   This . < Stringvalue1 > K _ backingfield;
}
[Compilergenerated]
Set
{
This . < Stringvalue1 > K _ backingfield = Value;
}
}

Public   Static   String Stringvalue2
{
[Compilergenerated]
Get
{
Return   < Stringvalue2 > K _ backingfield;
}
[Compilergenerated]
Set
{
< Stringvalue2 > K _ backingfield = Value;
}
}
}

 

 

ASP. NET development technology exchange group:67511751(Recruitment ...)

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.