SoFunction
Updated on 2025-04-11

C# Learning Basic Concepts Twenty-Five Questions Continued 2


What is the function of modifiers?
answer:
new modifier and new operator are two concepts
The new modifier is used to declare a class or a member of a class, indicating that a member of the same name is hidden in the base class. The new operator is used to instantiate a type
The new modifier can only be used for inheritance classes and is generally used to make up for the shortcomings of base class design.
The new modifier and the override modifier cannot be used on one member at the same time, because these two modifiers are mutually exclusive in meaning.
Example:
using System;
using ;
using ;
namespace Example09
{
    class BaseClass
    {
//The base class designer declares a public variable of a PI for easy operation
        public static double PI = 3.1415;
    }
    class DervieClass : BaseClass
    {
//Inheritance class finds that the value of this variable cannot meet the operation accuracy, so the declaration in the base class can be explicitly hidden through the new modifier.
        public new static double PI = 3.1415926;
    }
    class Program
    {
        static void Main(string[] args)
        {
            ();
            ();
            ();
        }
    }
}
result:
3.1415
3.1415926 

What is the meaning of keywords?
answer:
This is a reserved word, limited to use in constructor and method members
The class constructor appears in the class that represents the object being constructed itself, the class method represents the object that calls the method, the structure of the structure appears in the structure that represents the reference to the structure being constructed, and the structure method represents the reference to the result of calling the method.
This reserved words cannot be used in the implementation of static members because the object or structure is not instantiated at this time.
In the C# system, this  is actually a constant, so you cannot use this++ operation like this
This reserved word is generally used to limit hidden members of the same name, use the object itself as a parameter, declare the index accessor, and determine whether the object passed in the parameter is itself.
Example:
using System;
using ;
using ;
namespace Example10
{
    class Class1
    {
        private double c;
        private string value;
        public double C
        {
            get
            {
                return c;
            }
        }
        public Class1(double c)
        {
// Limited to hidden members of the same name
             = c;
        }
        public Class1(Class1 value)
        {
//There is no point in instantiating yourself with the object itself
            if (this != value)
            {
                c = ;
            }
        }
        public override string ToString()
        {
//Prefer the object itself as a parameter
            return ("{0} Celsius = {1} Fahrenheit", c, UnitTransClass.C2F(this));
        }
//Because of curiosity, I did an efficiency test here and wanted to see which way to access member variables faster. Conclusion: There is not much difference. . .
        public string Test1()
        {
            long vTickCount = ;
            for (int i = 0; i < 10000000; i++)
                 = ();
            return ("Have this.: {0} MSEL",  - vTickCount);
        }
        public string Test2()
        {
            long vTickCount = ;
            for (int i = 0; i < 10000000; i++)
                value = ();
            return ("Don't have this.: {0} MSEL",  - vTickCount);
        }
    }
    class UnitTransClass
    {
        public static double C2F(Class1 value)
        {
//Centimeter to Fahrenheit conversion formula
            return 1.8 *  + 32;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 tmpObj = new Class1(37.5);
            (tmpObj);
            (tmpObj.Test1());
            (tmpObj.Test2());
            ();
        }
    }
}
result:
37.5 Celsius = 99.5 Fahrenheit
Have this.: 4375 MSEL
Don't have this.: 4406 MSEL