Go C # Const and static readonly differences

Source: Internet
Author: User

As we all know, const and static readonly are really like: access through the class name instead of the object name, read-only in the program, and so on. Can be mixed in most cases.
The essential difference between the two is that the value of the const is determined during compilation, so its value can only be specified by a constant expression at the time of declaration. The static readonly is calculated at run time, so you can also assign a value by using a statically constructed function.
Having understood this essential difference, it is not difficult to see whether the static readonly and const in the following statement are interchangeable:

1.StaticReadOnly MyClass myins =NewMyClass ();2.StaticReadOnly MyClass myins =Null;3.StaticReadOnly A = B *20static readonly B = 10< Span style= "color: #000000;" >; 4. static readonly Span style= "color: #0000ff;" >int [] Constintarray = new int["{1, 2, 3};5. void SomeFunction () {int a = 10; ....}   

1: cannot be converted to Const. The new operator is required to execute the constructor, so it cannot be determined during compilation
2: can be converted to Const. We also see that constants of type reference (except string) can only be null.
3: Can be converted to Const. We can say very clearly during compilation that a equals 200.
4: cannot be converted to Const. The truth and 1 are the same, although it seems that the array of the one-way is really a constant.
5: Can not be replaced with readonly,readonly can only be used to modify the class field, can not modify local variables, and can not modify other class members such as property.

Therefore, you can use the static readonly for those places that are inherently constant but cannot be declared with Const.

Examples given in the C # specification include:

1PublicClassColor2{3PublicStaticReadOnly Color Black =New Color (0,0,0);4PublicStaticReadOnly Color white =New Color (255,255,255);5PublicStaticReadOnly Color Red =New Color (255,0,0);6PublicStaticReadOnly Color Green =New Color (0,255,0);7PublicStaticReadOnly Color Blue =New Color (0,0,255); 9 private byte red, green, Blue;11 public Color (byte R, byte G, byte b) 12  {13 red = R;14 Green = G; Blue = B; }17}        

One of the problems that static readonly needs to be aware of is that for a static readonly, the reference type is only limited to assignment (write) operations. The reading and writing of its members is 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 example above is not a class but a struct, then the two statements that follow will be faulted.

The difference between Const and static readonly:

Perhaps through the above-mentioned purely conceptual explanation, some beginners somewhat dizzy. Here are some examples to illustrate the following:

1UsingSystem;2ClassP3{4StaticReadOnlyint a=b*10 5 static readonly int b=10 6 public static void Main (string[] args)  { 8 Console.WriteLine ( "a is {0},b is {1} " Span style= "color: #000000;" >,A,B); }10}     

For the above code, what is the output? A lot of people would think it was a 100,b is 10! In fact, the correct output result is A was 0,b is 10.

Well, if you change to the following: using System;

1ClassP2{3Constint a=b*10 4 const int B=10 5 public static void Main (string[] args)  { 7 Console.WriteLine ( "a is {0},b is {1} " Span style= "color: #000000;" >,A,B); } 9 10}                 

For the above code, how much output is the result? Is it 0,b is 10? In fact, it is wrong, this time the correct output is a was 100,b is

So why is that so? In fact, the above said, const is a static constant, so at compile time will be a and B value down (that is, b variable 10, and a=b*10=10*10=100), then the output of the main function is of course A is 100,b is 10. The static readonly is a dynamic constant, the value of the variable is not parsed during compilation, so the start is the default value, like A and B are int type, ancient capital is 0. And in the execution of the program to A=B*10, so a=0*10=0, the program then executes to b=10 this sentence, only real B's initial value 10 is assigned to B.

Go C # Const and static readonly differences

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.