C # const, readonly, static readonly

Source: Internet
Author: User

Const defines a static value that is often assigned during object initialization. It cannot be changed later. It is a compilation constant. New cannot be used for initialization.

Readonly is a read-only variable. It is a runtime variable. You can change its value in the constructor class. It cannot act on local variables.

Const and static cannot be used together. It is already static.

We all know that const and static readonly are indeed very similar: accessing 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,The value of const is determined during compilation. Therefore, it can only be specified through a constant expression during declaration..

Static readonly,Read-only in the program, but itThe value is calculated during running, soStatic ConstructorTo assign values to it,

Readonly can only be used to modify the field of a class. It cannot modify local variables or modify other class members such as property.

After understanding the essential difference, we can easily see whether static readonly and const can be exchanged in the following statements:
1. Static readonly myclass myins = new myclass ();
2. Static readonly myclass myins = NULL;
3. Static readonly A = B * 20;
Static readonly B = 10;
4. Static readonly int [] constintarray = new int [] {1, 2, 3 };
5. Void somefunction ()
{
Const int A = 10;
...
}
1: cannot be changed to const. The new operator needs to execute the constructor, so it cannot be determined during compilation
2: can be changed to const. We can also see that constants of the reference type (except strings) can only be null.
3: can be changed to const. We can make it very clear during compilation that A equals 200.
4: cannot be changed to 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, static readonly can be used for those places that should be essentially constants but cannot be declared using Const. Example in C # specification:

Public class color
{
Public static readonly color black = new color (0, 0, 0 );
Public static readonly color white = new color (255,255,255 );
Public static readonly color red = new color (255, 0, 0 );
Public static readonly color green = new color (0,255, 0 );
Public static readonly 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;
}
}

Static readonly requires note that for a reference type of static readonly, the value assignment (write) operation is not allowed. The read and write operations on its members are still unrestricted.
Public static readonly 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.