The difference between const and readonly in C #

Source: Internet
Author: User

The concept of const is a variable that contains values that cannot be modified.
A constant expression is an expression that can be fully evaluated at compile-time. Therefore, constants cannot be initialized from values that are extracted from a variable.
If the const int a = B+1;B is a variable, it is obvious that the result cannot be computed at compile time, so constants cannot be initialized with variables.

ReadOnly allows you to set a field to a constant, but you can perform some operations to determine its initial value.
Because ReadOnly is executed at the time of calculation, of course it can be initialized with some variables.
ReadOnly are instance members, so different instances can have different constant values, which makes readonly more flexible.

The ReadOnly keyword differs from the const keyword.

1. The const field can only be initialized in the declaration of the field.
The readonly field can be initialized in a declaration or constructor. Therefore, depending on the constructor used, the ReadOnly field may have different values.
2. The const field is a compile-time constant, and the ReadOnly field can be used to run a constant number.
3. The const default is static, and readonly must display a declaration if it is set to static.
4. Const for a constant of a reference type, the possible value can be only string and null.
ReadOnly can be of any type

Summarize
Const can only be initialized with constants at an early stage. For each compiled result, the const value is fixed, and the value of ReadOnly can be determined at run time ~ ~






Const is a C-language keyword that restricts a variable from being allowed to be changed. The use of const to some extent can improve the security and reliability of the program, in addition, when watching other people's code, clearly understand the role of Const, to understand the other side of the program also has some help. In addition, the const appears in other programming languages, such as C + +, PHP5, c#.net, HC08 C
Const generic modified variable is read-only
The const definition should not change his value after it is initialized at the time of definition
Cases:

Copy CodeThe code is as follows: the const int a=1;//can only be initialized at the time of definition;
a=2;//error occurred because A is a read-only variable
The use of const to modify some pointers
int A;
const int *p=&a;//*p is not allowed to change but P is allowed to change
int * Const P1//Defines a read-only variable P1; P1 can not change *p1 can be changed

A constant declaration can declare multiple constants , for example:

Copy CodeThe code is as follows: Public Const Double x = 1.0, y = 2.0, z = 3.0;

The use of the static modifier in a constant declaration is not allowed.
Constants can participate in constant expressions, as follows:
Copy CodeThe code is as follows: Public const int c1 = 5;
public const INT C2 = c1 + 100;

Attention:

The ReadOnly keyword differs from the const keyword. The const field can only be initialized in the declaration of the field. The readonly field can be initialized in a declaration or constructor. Therefore, depending on the constructor used, the ReadOnly field may have different values. In addition, the Const field is a compile-time constant, and the ReadOnly field can be used to run a constant number, as shown in the following line of code:

Copy CodeThe code is as follows: public static readonly uint L1 = (uint) datetime.now.ticks;

The difference between const and readonly in C #

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.