Fields, properties, index ers, and const ants in C #

Keywords: C++ C#

The essence of a program is "data + algorithm". The following four members are used to represent "data".

field:

1. What is a field

(1) A field is a variable that represents an association with an object or type (class and structure)

The essence of a field is a variable, which is used to store data. A field is used to store data for an object or type. Different from the local variables in the method body, the local variables in the method body help the method store data temporarily when the method is running, while the field helps the object or type store data. This is true when an object or type has multiple fields The values of these fields can be combined to represent the current state of the object or type.   So simply put, fields are used to store data for objects or types, and the combination of multiple fields can express the state of objects or types.

(2) A field is a member of a type, commonly referred to as a member variable“

Fields are essentially variables and members of objects or types, so they are generally called "member variables"“

(3) The fields associated with an object are also called instance fields“

(4) The field associated with the type is called a "static field" and is decorated by the "static" modifier

”The difference between instance field and static field is that the combination of instance field represents the state of the object associated with it, while static field represents the state of the type associated with it (saving data for a data type).

   After instantiating a class, you can access its publicly accessible data type (instance field), which can be used to represent the state of the instance.

  The instance field stores data for the instance, and the combination of data represents the current state of the instance.

class Program
    {
        static void Main(string[] args)
        {
            Student student1 = new Student();//Create an instance of Student student1
            student1.Age = 40;
            student1.Score = 90;//The combination of instance field storage data represents the status of the current instance

            Student student2 = new Student();//Create an instance of Student student2
            student1.Age = 40;
            student1.Score = 40;//The combination of instance field storage data represents the status of the current instance
        }
    }

    class Student //Declare a class
    {
        //Declare "data type" for students, create members, and public is public access
        public int Age;
        public int Score;

        //Declare "static field" members for students, public means public access, and static means static
        public static int AvarageAge;
        public static int AvarageScore;
        public static int Amount;
        
        public Student() //Constructor
        {
            //Add a logic to the constructor. Each time you reference Student to create an instance, the Amount will be + 1
            Student.Amount++;
        }

        public static void ReportAmount()//Create a static method
        {
            Console.WriteLine(Amount);
        }
    }

  Both static fields and static methods can be accessed when the reference class creates an instance.

Static fields, static methods and instance fields in the class can be accessed only when an instance is created by referencing this class. Static fields can call methods directly without creating an instance. For example:

Constructor: constructor is a special method. It is mainly used to initialize an object when creating an object, that is, an object Member variable Assign initial value, total and new operator A special class can have multiple constructors, which can be distinguished according to the number of parameters or parameter types heavy load.

 

  2. Declaration of field:

Declaration field syntax:

  A field is a member of a class, but it is not a statement. Therefore, when declaring a field, it should be written in the "class body" instead of the "method body" or "method body" (that becomes a "local variable").

The syntax of the field declaration with "opt" subscript is optional or negligible.

(properties / attributes):  

(field modifier): it can be one or more, but it must be a meaningful modifier combination, such as public/    public static; public and private cannot be used at the same time.

Where the field is  ” The field modified by "readonly" (read-only modifier) is called "read-only field", so you can only read the value in the field and cannot modify it.

readonly (read-only modifier): read only field initialization is to prevent the value after initialization from being changed or reassigned

        (1) Read only instance field: the output value is "1"“

There is only one chance to initialize the read-only instance field, that is, initialize it in the constructor (constructor) of the read-only instance field variable (or initialize it when declared), pass the value to the constructor when creating the instance, and then initialize the read-only instance field. The static instance field initialization event occurs when the object is created (when instantiating).

class Program
    {
        static void Main(string[] args)
        {
            Student student1 = new Student(1);
            
          // student1.ID = 3; / / an error will be reported here. You can no longer assign values to the variables initialized in the read-only instance field
            
            Console.WriteLine(student1.ID);//The output is "1"“
        }
    }

    class Student //Declare a class
    {      
       
        public readonly int ID;  //Read only instance field
        
        public Student(int id)   //Constructor
        {
            this.ID = id;
        }

    }

        (2) Read only static field: the first is to initialize the value when declaring the static read-only field; the latter is to create a static constructor after declaring the static read-only field. The initialization event of the static field occurs when the type is loaded

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Brush.DefultColor.R);
            Console.WriteLine(Brush.DefultColor.G);
            Console.WriteLine(Brush.DefultColor.B);
            //Output three zeros

            Brush.DefultColor = new Color() { R=255,G=255,B=255};
            //The error "unable to assign value to static read-only field" will be reported here
        }
    }

    struct Color  //Create a constructor
    {
        public int R;
        public int G;
        public int B;

    }
    class Brush  //Create a class with a static read-only instance, which is explicitly initialized when instantiated
    {
        public static readonly Color DefultColor = new Color() { R = 0, G = 0, B = 0 };
    }

------------------------------If you write it the same way, you will also report an error

    class Brush  //Create a class with a static read-only instance, which is explicitly initialized when instantiated
    {
        public static readonly Color DefultColor;
        
        static Brush()  //Constructor (the previous public is changed to static to become a static constructor)
        {
            Brush.DefultColor = new Color() { R = 0, G = 0, B = 0 };
        }
    }

(data type): byte, short, int, long, float, double, bool, char, etc.

(variable declarator): it can be "variable name" or "variable name plus variable initializer (that is, an assignment symbol plus an initial value)".

tips: initializing a field when it is declared is the same as initializing it in the constructor. It is recommended to initialize the field when it is declared for easy identification in the future.

 

 

 

 

 

 

 

 

 

 

 

Posted by ziggy3000 on Fri, 24 Sep 2021 08:57:16 -0700