Difference between readonly and const: readonlyconst

Source: Internet
Author: User

Difference between readonly and const: readonlyconst

Const and static readonly are indeed very similar: when accessing through the class name rather than the object name, the variable is declared as read-only and cannot be rewritten after the variable is initialized,

In most cases, they can be mixed.

The essential difference between the two lies in the distinction between Static and Dynamic Constants when it involves the essence:

Compile-time constants: a static constant refers to a constant that the compiler parses during compilation and replaces the value of the constant with the value of the initialization.

Runtime constants: the value of a dynamic constant is obtained at the moment of running. during compilation, the value is marked as a read-only constant instead of a constant value, in this way, Dynamic Constants do not need to be initialized during declaration, but can be delayed to initialization in the constructor.

The two have different characteristics. Incorrect use will not only result in loss of efficiency, but also cause errors.

The value of const is determined during compilation. Therefore, it can only be specified through a constant expression during Declaration, so it complies with static constants.

While readonly calculates its value during running, the modified constant will be delayed until running.

Therefore, you can assign values through static constructors.

 

Const int num = 10; // here the value of static readonly int num = 10 is determined for num when the main method is started for the first time; // when readonly is used, a fixed value is assigned only when the program reaches all parties in the region.


// The const modifier constant has calculated the nominal value of num during compilation, while the static readonly modifier constant has not been resolved.

 

 

Static readonly

In addition, the const constant can be declared in the class or in the function body, but the static readonly constant can only be declared in the class.

After understanding the essential difference, we will know whether static readonly and const can be exchanged in the statement,

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.