Const and readonly

xiaoxiao2021-03-06  75

A constant expression is an expression That Can Be Fully Evaluated At Compile Time. Therefore, The Only Possible Values ​​for Constants of Reference Types Are String and Null.

So C # actually only support: const string cstr = "hello, world"; const int = null; // This is not practical. STATIC READONLY means that when compiling, does not confirm the value, when you run, you will not change it after the first assignment. Const means that it is confirmed when compiling. In terms of performance, Const is generally directly copy to reference during compilation, and Static Readonly is still a reference to a variable. So if this variable is defined in a different assemblyMbly, const can be less than an indirect addressing of Assembly Loading procedures and variables. ------------------------------------------

The value of const and readonly can no longer be rewritten once initialization; Const can only initialize the declaration; readOnly can initialize both in the declaration or initialization in the constructor; constiTic STATIC, can no longer write static const; readonly If you don't default, Static, if you need to write static readonly; consT is a constant of compile-static resolution (so its expression must be evaluated when compiling); readonly is a constant of dynamic resolution of runtime; Const can be used to modify Members in the class, or the local variables in the function; Readonly can only be used in the modified members. --------------------------------------- Readonly keyword is a modification that can be used on the field symbol. When the field declaration includes the ReadOnly modifier, the field assignment introduced by the declaration can only be taken as part of the declaration, or in the same class constructor. The value of the read-only field can only be performed in the following context:

When the variable is initialized in the declaration, for example: public readonly int y = 5; for the instance field, in the instance constructor of the class containing the field declaration; or for a static field, in the static constructor of the class containing the field declaration . The ReadOnly field is only valid for the ReadOnly field to OUT or REF parameters when in these contexts. Example // cs_readonly_keyword.cs // Readonly fieldsusing System; public class ReadOnlyTest {class MyClass {public int x; public readonly int y = 25; // Initialize a readonly field public readonly int z; public MyClass () {z = 24; // Initialize a readonly instance field} public myclass (int P1, int p2, int p3) {x = p1; y = p2; z = p3;}} public static void main () {Myclass P1 = New Myclass (11, 21, 32); // ok console.writeline ("p1: x = {0}, y = {1}, z = {2}", p1.x, p1.y, p1.z); MyClass P2 = New myclass (); p2.x = 55; // ok console.writeline ("p2: x = {0}, y = {1}, z = {2}", p2.x, p2.y, p2. z);}} Output P1: X = 11, y = 21, Z = 32p2: x = 55, y = 25, z = 24 In the previous example, if such a statement is used: P2.Y = 66; / / Error will receive a compiler error message: The Left-hand side of an association is the same as that attempt to assign a value to a constant.

Note that readonly keywords

Const

Key words are different. The Const field can only initialize in the declaration of this 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 compiled, and the readOnly field can be used to run the time, as shown in the following example:

Public static readonly uint l1 = (uint) DateTime.now.ticks;

转载请注明原文地址:https://www.9cbs.com/read-107617.html

New Post(0)