Compare readonly and const in C #

Source: Internet
Author: User

C # has two constant types: readonly (runtime constant) and const (compilation constant ), this article compares the two types of features and describes their application scenarios.

Working Principle
Readonly is the runtime constant. It is assigned a value when the program is running. It cannot be changed after the value is assigned. Therefore, some people call it a read-only variable.
Const is the compile-time constant. During program compilation, constant values are parsed and all constant references are replaced with corresponding values.
The following two constants are declared:

Supported Data Types

Const constants are replaced with literal values during compilation, which limits their value types. Const constants can only be assigned numbers (integers, floating-point numbers), strings, and enumeration types. The following code cannot be compiled:

Maintainability

Readonly works in reference mode. After a constant is updated, all the places that reference this constant can get the updated value.
Const is a little more complex, especially for cross-assembly calls:
 

Assume that Class1 and Class2 are in two different sets, and now change the constant value in Class1:

Performance Comparison
Const is directly involved in the operation in literal form, and the performance is slightly higher than readonly, but for general applications, this performance difference can be said to be minimal.

Applicable scenarios
In the following two cases:
A. The value remains unchanged permanently (such as the circumference rate, the number of hours in a day, and the Earth's radius)
B. Strict Program Performance Requirements
You can use the const constant. In other cases, the readonly constant should be used first.

 

Public class Class1
{
Public static readonly int A = 4; // A indicates the running time.
Public const int B = 5; // B indicates the compile time.
}

Compile and deploy Class1 (Note: Class2 is not re-compiled at this time), and check the value of variable C again:

Console. WriteLine (Class2.C); // output "7"

The result may be a little unexpected. Let's take a closer look at the value assignment expression of variable C:

Public static int C = Class1.A + Class1. B;

After compilation, it is equivalent to the following form:

Public static int C = Class1.A + 3;

Therefore, no matter how the value of constant B changes, it will not affect the final result. Although re-compiling Class2 can solve this problem, at least we can see the possible maintenance problems caused by const.

 

Public class Class1
{
Public
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.