Virtual can be seen in many OOP languages such as C++ and Java, and C# is of course no exception as a completely object-oriented language.
From the perspective of C# program compilation, what is the difference between virtual functions and other general functions? Generally, functions are statically compiled into the execution file during compilation, and their relative addresses do not change during the program's running, which means they are written to death! The virtual function is not compiled statically during compilation, and its relative address is uncertain. It will dynamically judge the function to be called based on the object instance during the runtime. The class defined at the declaration is called the declaration class, and the class instantiated at the execution is called the instance class.
For example: bird bird = new sparrow();
Then birds are declarations and sparrows are instances.
The specific inspection process is as follows:
1. When calling an object's function, the system will directly check the class defined by the object declaration, that is, the declaration class, to see if the called function is a virtual function;
2. If it is not a virtual function, then it executes the function directly. If there is a virtual keyword, which is a virtual function, then it will not execute the function immediately at this time, but will instead check the object's instance class.
3. In this instance class, it will check whether the virtual function is re-implemented (through the override keyword) in the definition of this instance class. If it is, then OK, it will not look for it again, and immediately execute the re-implemented function in the instance class. If not, the system will keep looking for the parent class of the instance class upwards, and repeat the checks in the instance class just now on the parent class until it finds the first parent class that overloads the virtual function, and then executes the overloaded function in the parent class.
Knowing this, you can understand the running results of the following code:
using System; namespace VirtualMethodTest { class A { public virtual void Func() // Note virtual, indicating that this is a virtual function { ("Func In A"); } } class B : A // Note that B inherits from Class A, so A is the parent class and B is the child class { public override void Func() // Pay attention to override, indicating that the virtual function has been reimplemented { ("Func In B"); } } class C : B // Note that C inherits from Class A, so B is the parent class and C is the child class { } class D : A // Note that B inherits from Class A, so A is the parent class and D is the child class { public new void Func() // Note that new, indicating that the class with the same name in the parent class is overwritten, rather than re-implementation { ("Func In D"); } } class program { static void Main() { A a; // Define an object of class A, this A. This A is the declaration class of a A b; // Define an object of class A, b. This A is the declaration class of b A c; // Define an object of class A, c. This A is the declaration class of b A d; // Define an object of class A, d. This A is the declaration class of b a = new A(); // Instantiate the object a, A is the instance class of a b = new B(); // Instantiate the b object, B is the instance class of b c = new C(); // Instantiate the b object, C is the instance class of b d = new D(); // Instantiate the b object, D is the instance class of b (); // Execution: 1. Check the declared class A first 2. Check that it is a virtual method 3. Go to check the instance class A, it is itself 4. Execute the method in the instance class A 5. Output result Func In A (); // Execution: 1. Check the declaration class A first 2. Check that it is a virtual method 3. Go to check instance class B, which has overloaded 4. Execute the method in instance class B 5. Output result Func In B (); // Execution: 1. Check the declaration class A first 2. Check that it is a virtual method 3. Turn to check the instance class C, no overload 4. Turn to check the parent class B of class C, with overload 5. Execute the Func method in parent class B 5. Output result Func In B (); // Execution: 1. Check the declaration class A first. 2. Check that it is a virtual method. 3. Turn to check the instance class D, without overloading (note this place. Although D implements Func(), the override keyword is not used, so it will not be considered an overload) 4. Turn to check the parent class A of class D, just for itself. 5. Execute the Func method in parent class A 5. Output result Func In A D d1 = new D(); (); // Execute Func() in class D and output the result Func In D (); } } }
The above is the detailed explanation of the virtual method of C# virtual. For more information about virtual method of C#, please follow my other related articles!