C # difference between const and static readonly

Source: Internet
Author: User

We all know that const and static readonly are indeed very similar: access by class name rather than Object Name, read-only in the program, and so on. In most cases, they can be mixed.
The essential difference between the two is that the const value is determined during compilation, so it can only be specified through a constant expression during declaration. Static readonly calculates its value during running, so it can be assigned a value through a static constructor.
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: It can be changed to const. We can clearly say that A equals 200 during the compilation process.
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: it cannot be changed 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:
 
 
1 public class Color
2 {
3 public static readonly Color Black = new Color (0, 0, 0 );
4 public static readonly Color White = new Color (255,255,255 );
5 public static readonly Color Red = new Color (255, 0, 0 );
6 public static readonly Color Green = new Color (0,255, 0 );
7 public static readonly Color Blue = new Color (0, 0,255 );
8
9 private byte red, green, blue;
10
11 public Color (byte r, byte g, byte B)
12 {
13 red = r;
14 green = g;
15 blue = B;
16}
17}
 
 
 
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.
 
 
Difference between Const and static readonly:

Some beginners may be confused by the above purely conceptual explanation. The following are some examples:
 
 
1 using System;
2 class P
3 {
4 static readonly int A = B * 10;
5 static readonly int B = 10;
6 public static void Main (string [] args)
7 {
8 Console. WriteLine ("A is {0}, B is {1}", A, B );
9}
10}
 
 
What is the output result of the above Code? Many people think that A is 100 and B is 10! In fact, the correct output result is A is 0 and B is 10.
 
Well, if it is changed to the following: using System;
 
 
1 class P
2 {
3 const int A = B * 10;
4 const int B = 10;
5 public static void Main (string [] args)
6 {
7 Console. WriteLine ("A is {0}, B is {1}", A, B );
8}
9
10}
 
 
What is the output result of the above Code? Is it A is 0, B is 10? Actually again wrong. The correct output result is A is 100, B is

So why? As mentioned above, const is A static constant, so the values of A and B are determined during compilation (that is, when the value of B is 10, and A = B * 10 = 10*10 = 100), the output in the Main function is of course A is 100, B is 10. Static readonly is A dynamic constant. The variable value is not parsed during compilation. Therefore, it is the default value at the beginning. For example, if both A and B are of the int type, they are all 0. When the program runs to A = B * 10; so A = 0*10 = 0, and the program then runs to B = 10, the true initial value 10 of B is assigned to B.
 
We all know that const and static readonly are indeed very similar: access by class name rather than Object Name, read-only in the program, and so on. In most cases, they can be mixed.
The essential difference between the two is that the const value is determined during compilation, so it can only be specified through a constant expression during declaration. Static readonly calculates its value during running, so it can be assigned a value through a static constructor.
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: It can be changed to const. We can clearly say that A equals 200 during the compilation process.
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: it cannot be changed 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:
 
 
1 public class Color
2 {
3 public static readonly Color Black = new Color (0, 0, 0 );
4 public static readonly Color White = new Color (255,255,255 );
5 public static readonly Color Red = new Color (255, 0, 0 );
6 public static readonly Color Green = new Color (0,255, 0 );
7 public static readonly Color Blue = new Color (0, 0,255 );
8
9 private byte red, green, blue;
10
11 public Color (byte r, byte g, byte B)
12 {
13 red = r;
14 green = g;
15 blue = B;
16}
17}
 
 
 
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 www.2cto.com
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.
 
 
Difference between Const and static readonly:

Some beginners may be confused by the above purely conceptual explanation. The following are some examples:
 
 
1 using System;
2 class P
3 {
4 static readonly int A = B * 10;
5 static readonly int B = 10;
6 public static void Main (string [] args)
7 {
8 Console. WriteLine ("A is {0}, B is {1}", A, B );
9}
10}
 
 
What is the output result of the above Code? Many people think that A is 100 and B is 10! In fact, the correct output result is A is 0 and B is 10.
 
Well, if it is changed to the following: using System;
 
 
1 class P
2 {
3 const int A = B * 10;
4 const int B = 10;
5 public static void Main (string [] args)
6 {
7 Console. WriteLine ("A is {0}, B is {1}", A, B );
8}
9
10}
 
 
What is the output result of the above Code? Is it A is 0, B is 10? Actually again wrong. The correct output result is A is 100, B is

So why? As mentioned above, const is A static constant, so the values of A and B are determined during compilation (that is, when the value of B is 10, and A = B * 10 = 10*10 = 100), the output in the Main function is of course A is 100, B is 10. Static readonly is A dynamic constant. The variable value is not parsed during compilation. Therefore, it is the default value at the beginning. For example, if both A and B are of the int type, they are all 0. When the program runs to A = B * 10; so A = 0*10 = 0, and the program then runs to B = 10, the true initial value 10 of B is assigned to B.

 

 

From yagene

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.