C # Various field type comparison

1. Comparison of constants, read-only fields, static fields and static read-only fields

 1  public class ModelClass

2 {
3 //The constant must be assigned an initial value when it is defined
4 //public const string constField;
5 public const string constField = "Constant";
6 public readonly string readField = "Read-only field";
7 public static string staticField = "Static field";
8 public static readonly string staticReadField = "Static read-only field"< span style="color: #000000;">;
9
10 public ModelClass()
11 {
12 //The value of the constant must be known at compile time, and the constructor is executed at runtime, so the constant cannot be assigned an initial value through the constructor; and the value of the read-only field can be determined at runtime.
13 //constField = "Cannot initialize constants in the constructor";
14 readField = "Constructor initializes read-only fields";
15 }
16 static ModelClass()
17 {
18 //constField = "Cannot initialize constant in static constructor";
19 staticField = "Static constructor initializes static fields";
20 staticReadField = "Static constructor initializes static read-only fields";
21 }
22
23 public string Method()
24 {
25 //Define and use constants in methods
26 const string constLocal = "Local constant";
27 string result = constLocal;
28 return result;
29 //Neither readonly nor static can be used in methods
30 }
31 public static string StaticMethod()
32 {
33 //Define and use constants in static methods
34 const string constLocal = "Local constant";
35 string result = constLocal;
36 return result;
37 //Neither readonly nor static can be used in static methods
38 }
39 }
40 public class RealizeObject
41 {
42 public void Realize()
43 {
44 //Constants, static fields, and static read-only fields are at the class level
45 string value1 = ModelClass.constField;
46 string value2 = ModelClass.staticField;
47 string value3 = ModelClass.staticReadField;
48 //Read-only fields are object-level
49 ModelClass model = new ModelClass();
50 string value4 = model.readField;
51 //The values ​​of constants, read-only fields, and static read-only fields cannot be modified
52 //ModelClass.constField = "Cannot modify the value of a constant";
53 //model.readField = "You cannot modify the value of a read-only field";
54 //ModelClass.staticReadField = "Cannot modify the value of a static read-only field";
55 ModelClass.staticField = "You can modify the value of a static field";
56 }
57 }

Constant, read-only field, static field and static read-only field Comparison table:

share pictures

Applicable data for constants, read-only fields, static fields, and static read-only fields:

1. The constants are known at the time of definition and Data that cannot be changed.

2. Read-only fields are suitable for data that can be assigned at runtime by a third party and cannot be changed (object exclusive).

3. Static read-only fields are suitable for data that can be assigned at runtime by a third party and cannot be changed (object sharing).

4. Static fields apply to data shared by objects.

 1 public class ModelClass

2 {
3 //The constant must be assigned an initial value when it is defined
4 //public const string constField;
5 public const string constField = "Constant";
6 public readonly string readField = "Read-only field";
7 public static string staticField = "Static field";
8 public static readonly string staticReadField = "Static read-only field"< span style="color: #000000;">;
9
10 public ModelClass()
11 {
12 //The value of the constant must be known at compile time, and the constructor is executed at runtime, so the constant cannot be assigned an initial value through the constructor; and the value of the read-only field can be determined at runtime.
13 //constField = "Cannot initialize constants in the constructor";
14 readField = "Constructor initializes read-only fields";
15 }
16 static ModelClass()
17 {
18 //constField = "Cannot initialize constant in static constructor";
19 staticField = "Static constructor initializes static fields";
20 staticReadField = "Static constructor initializes static read-only fields";
21 }
22
23 public string Method()
24 {
25 //Define and use constants in methods
26 const string constLocal = "Local constant";
27 string result = constLocal;
28 return result;
29 //Neither readonly nor static can be used in methods
30 }
31 public static string StaticMethod()
32 {
33 //Define and use constants in static methods
34 const string constLocal = "Local constant";
35 string result = constLocal;
36 return result;
37 //Neither readonly nor static can be used in static methods
38 }
39 }
40 public class RealizeObject
41 {
42 public void Realize()
43 {
44 //Constants, static fields, and static read-only fields are at the class level
45 string value1 = ModelClass.constField;
46 string value2 = ModelClass.staticField;
47 string value3 = ModelClass.staticReadField;
48 //Read-only fields are object-level
49 ModelClass model = new ModelClass();
50 string value4 = model.readField;
51 //The values ​​of constants, read-only fields, and static read-only fields cannot be modified
52 //ModelClass.constField = "Cannot modify the value of a constant";
53 //model.readField = "You cannot modify the value of a read-only field";
54 //ModelClass.staticReadField = "Cannot modify the value of a static read-only field";
55 ModelClass.staticField = "You can modify the value of a static field";
56 }
57 }

Leave a Comment

Your email address will not be published.