C# Contrast of Various Field Types

I. Comparison of Constant, Read-Only, Static and Static Read-Only Fields

 1 public class ModelClass
 2 {
 3     //Constants must be defined with initial values
 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 fields";
 9 
10     public ModelClass()
11     {
12         //Constant values must be known at compile time, and constructors are executed at run time, so constants cannot be initialized by constructors; the values of read-only fields can be determined at run time.
13         //constField = "Constants cannot be initialized in constructors";
14         readField = "Constructor initializes read-only fields";
15     }
16     static ModelClass()
17     {
18         //constField = "Constants cannot be initialized in static constructors";
19         staticField = "Initialization of static fields by static constructors";
20         staticReadField = "Initialization of static read-only fields by static constructors";
21     }
22 
23     public string Method()
24     {
25         //Define constants in methods and use them
26         const string constLocal = "Local constant";
27         string result = constLocal;
28         return result;
29         //readonly and static Neither can be used in the method.
30     }
31     public static string StaticMethod()
32     {
33         //Define constants in static methods and use them
34         const string constLocal = "Local constant";
35         string result = constLocal;
36         return result;
37         //readonly and static Cannot 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 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 = "Constant values cannot be modified";
53         //model.readField = "You cannot modify the value of a read-only field";
54         //ModelClass.staticReadField = "You cannot modify the value of a static read-only field";
55         ModelClass.staticField = "You can modify the values of static fields";
56     }
57 }

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

Constants, read-only fields, static fields and static read-only fields apply to data:

1. Constants apply to data that are known at definition and cannot be changed.

2. Read-only fields are applicable to data that can not be changed at runtime by a third party (object exclusive).

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

4. Static fields are suitable for data shared by objects.

Keywords: C#

Added by rm_phpbuilder on Thu, 03 Oct 2019 04:18:20 +0300