What is the static field and instance field of C#
This article mainly introduces "what is the static field and instance field of C#". In the daily operation, I believe that many people have doubts about what the static field and instance field of C# are. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "what is the static field and instance field of C#". Next, please follow the editor to study!
C# static field and C# instance field
There are two kinds of fields: C# static field and C# instance field. The field is passed by reference and can be of any type.
Example:
Private static int i = 0; / / name a static field private int j = 0; / / name an instance field static void Main (string [] args) {Program a = new Program (); / / create an object reference and instantiate it. Console.WriteLine (a.j); / / use the object to access the field j Console.WriteLine (Program.i); / / static fields need to be accessed with the class name}
We can see from the example that the static field belongs to the class and the instance field belongs to the object.
Readonly Guanjian words:
The readonly Guanjian word is used to declare a read-only field, that is, the field is not allowed to be rewritten, but with one exception, in the constructor, the read-only field can be rewritten.
Example:
Class Program {private readonly int i = 0 new Program / name A read-only field private readonly int j = 0; Program () {this.j = 10 strike / rewrite field j in the constructor} static void Main (string [] args) {Program a = new Program (); / / create an object reference and instantiate it. / / A.I = 10ramp / here is the error report Console.WriteLine (A.J); Console.WriteLine (A.I);}} at this point, the study of "what is the static field and instance field of C#" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!