SoFunction
Updated on 2025-03-07

Related usage methods of virtual functions in c#

If an instance method declaration is preceded by virtual keyword, then this method is a virtual method.

The biggest difference between virtual methods and non-virtual methods is that the implementation of virtual methods can be replaced by derived classes, which is implemented through method rewriting (to be discussed later)
Features of virtual methods:
Static, abstract, or override modifiers are not allowed before virtual methods
Virtual methods cannot be private, so private modifiers cannot be used.
Execution of virtual methods:
We know that general functions are statically compiled into the execution file during compilation, and their relative addresses do not change during the program run.
The virtual function is not statically compiled 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: A a =new B(); where A is the declaration class and B is the instance class.
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 it is a virtual function, then it will not execute the function immediately at this time, but will start to check the instance class of the object.
3. In this instance class, it will check whether there is a method to implement the virtual function or re-implement the virtual function (through the override keyword) in the definition of this instance class.
If there is, it will not look for it again, but will immediately execute the method of virtual functions implemented in the instance class. If not, the system will keep looking for the parent class of the instance class.
And repeat the checks in the instance class just now for the parent class until the first parent class overloaded the virtual function is found, and then execute the overloaded function in the parent class.
Example 1:

Copy the codeThe code is as follows:

class A
    {
        publicvirtualvoid Sum()
        {
            ("I am A Class,I am virtual sum().");
        }
    }
    class Program
    {
        staticvoid Main(string[] args)
        {
A a=new A();   // Define an object of class A, this A. This A is the declaration class of a, instantiates object a, and A is the instance class of a
             ();
             ();
        }
    }

implement:
1. Check the declaration class A first. 2. Check that it is sum and it is a virtual method. 3. Turn to check the instance class A, and the result is the question itself.
4. Execute the method to implement Sum in instance class A 5. Output result I am A Class,I am virtual sum().
Example 2:

Copy the codeThe code is as follows:

class A
    {
        publicvirtualvoid Sum()
        {
            ("I am A Class,I am virtual sum().");
        }
    }
    class B : A   
    {
publicoverridevoid Sum() // Reimplemented virtual function
        {
            ("I am B Class,I am override sum().");
        } 

    }
    class Program
    {
        staticvoid Main(string[] args)
        {
A a=new B();  // Define an object of class A, this A. This A is the declaration class of a, instantiates object a, and B is the instance class of a
             ();
             ();
        }
    }


implement:
1. Check the declared class A first. 2. Detect it is a virtual method. 3. Go to check the instance class B. There is a rewritten method. 4. Execute the method in the instance class B. 5. Output result I am B Class,I am override sum().
Example 3:
Copy the codeThe code is as follows:

class A
    {
        publicvirtualvoid Sum()
        {
            ("I am A Class,I am virtual sum().");
        }
    }
    class B : A   
    {
publicoverridevoid Sum() // Reimplemented virtual function
        {
            ("I am B Class,I am override sum().");
        } 

    }
    class C : B
    {

    }
    class Program
    {
        staticvoid Main(string[] args)
        {
A a=new C();// Define an object of class A, a. This A is the declaration class of a, instantiating object a, C is the instance class of a
             ();
             ();
        }
    }


implement:
1. Check the declared class A first. 2. It is detected as a virtual method. 3. Turn to check the instance class C, there is no rewrite method. 4. Turn to check the parent class B of class C, there is a rewrite method.
5. Execute the Sum method in parent class B 6. Output result I am B Class,I am override sum().
Example 4:

Copy the codeThe code is as follows:

class A
    {
        publicvirtualvoid Sum()
        {
            ("I am A Class,I am virtual sum().");
        }
    }
    class B : A   
    {
publicnewvoid Sum() //Overwrite the function of the same name in the parent class, rather than re-implement it
        {
            ("I am B Class,I am new sum().");
        } 

    }
    class Program
    {
        staticvoid Main(string[] args)
        {
             A a=new B();
             ();
             ();
        }
    }


implement:
1. Check the declaration class A first. 2. Check that it is a virtual method. 3. Turn to check instance class B, without rewriting (note this place. Although B implements Sum(), the override keyword is not used, so it will not be considered a rewrite) 4. Turn to check the parent class A of class B, just for itself 5. Execute the Sum method in parent class A 6. Output result I am A Class, I am virtual sum().
So what if in Example 4, what is stated is a class B?
Copy the codeThe code is as follows:

class A
    {
        publicvirtualvoid Sum()
        {
            ("I am A Class,I am virtual sum().");
        }
    }
    class B : A   
    {
publicnewvoid Sum() //Overwrite the function of the same name in the parent class, rather than re-implement it
        {
            ("I am B Class,I am new sum().");
        } 

    }
    class Program
    {
        staticvoid Main(string[] args)
        {
             B b=new B();
             ();
             ();
        }
    }


Execute Sum() in Class B and output the result I am B Class, I am new sum().
Can abstract functions be used to rewrite virtual functions in base classes?
The answer is yes.

Copy the codeThe code is as follows:

class A
    {
        publicvirtualvoid PrintFriends()
        {
            ("()");  
        } 
    }
    abstractclass B : A   
    {
public abstract override void PrintFriends();   //Use the override modifier to indicate that the implementation of the function in the base class is rewrited in the abstract
    }
    abstract class C : A
    {
public abstract new void PrintFriends();         //Use the new modifier to explicitly declare the implementation of the function in the base class.
    }

Can sealed classes have virtual functions?
Yes, virtual functions in the base class will be implicitly converted into non-virtual functions, but the sealed class itself cannot add new virtual functions.

Copy the codeThe code is as follows:

class A
    {
        publicvirtualvoid Fun()
        {
            ("I am A.");
        }
    }
    sealedclass Program:A
    {
        public override void Fun()
        {
            ("I am B.");
        }
        staticvoid Main(string[] args)
        {
            Program p =new Program();
            ();
            ();
        }
    }