C # differences between static const and readonly

Source: Internet
Author: User

From: http://blog.csdn.net/lai123wei/article/details/6933340

 

 

ConstIt defines the static value that is often assigned during object initialization. It cannot be changed later. It is a compilation constant.

StaticDefines static variables. You can change its value externally ..

Readonly is a read-only variable. It belongs to the runtime variable. It can be changed during class initialization ..

ConstAndStaticCannot be used together

We all know that, Const And Static Readonly is indeed very similar: access through the class name rather than the object name, read-only in the program, and so on. In most cases, it can be mixed.
The essential difference between the two lies in, Const The value is in During Compilation So the value can only be specified through a constant expression during declaration. . While Static Readonly is The value is calculated during running, so the value can be assigned through the static constructor.
By understanding the essential difference, we can easily see the following statement Static Readonly and Const Can it be exchanged:
1. Static Readonly myclass myins = new myclass ();
2. Static Readonly myclass myins = NULL;
3. Static Readonly int A = B * 20;
Static Readonly int B = 10;
4. Static Readonly int [] constintarray = new int [] {1, 2, 3 };
5. Void somefunction ()
{
Const Int A = 10;
...
}
1: cannot be replacedConst . The new operator needs to execute the constructor, so it cannot be determined during compilation
2: can be replaced Const . We can also see that constants of the reference type (except strings) can only be null.
3: can be changed Const . We can make it very clear during compilation that A equals 200.
4: cannot be replaced Const . The principle is the same as that of 1, although it seems that the arrays of 1, 2, and 3 are indeed constants.
5: you cannot change to readonly. readonly can only be used to modify the field of the class. It cannot modify local variables or other class members such as property.

Therefore, for those that are essentially constants, but cannot be usedConstCan be used to declareStaticReadonly. For exampleC #Example given in the Specification:

Public class color
{
PublicStaticReadonly color black = new color (0, 0, 0 );
PublicStaticReadonly color white = new color (255,255,255 );
PublicStaticReadonly color red = new color (255, 0, 0 );
PublicStaticReadonly color green = new color (0,255, 0 );
PublicStaticReadonly color blue = new color (0, 0,255 );

Private byte red, green, blue;

Public color (byte R, byte g, byte B)
{
Red = R;
Green = g;
Blue = B;
}
}

StaticReadonly needs to pay attention to the following problems:StaticReadonly reference type, but cannot be assigned (written. The read and write operations on its members are still unrestricted.
PublicStaticReadonly myclass myins = new myclass ();
...
Myins. someproperty = 10; // normal
Myins = new myclass (); // error. The object is read-only.

However, if the myclass in the above example is not a class but a struct, then the following two statements will go wrong.

 

 

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.