Polymorphism in C #

I believe that everyone is familiar with the three characteristics of object-oriented packaging, inheritance, and polymorphism. Everyone can say one or two sentences, but most of them just know what these are and do not know how the CLR is implemented internally, so This article focuses on the mechanisms by which some concepts in polymorphism have been implemented internally.

First, the concept of polymorphism
First explain what polymorphism is: the same operation on different objects can have different interpretations and produce different execution results. This is polymorphism. In other words, actually calling the same method on an instance of the same type produces different results. The "same" here is double-quoted because the same method here is just the method that looks the same, in fact they call different methods.
 
Speaking of polymorphism, we cannot mention the following concepts freely: overloading, rewriting, virtual methods, abstract methods, and hidden methods. Let's introduce their concepts one by one.
1, overload (overload): two or more methods in the same scope (generally refers to a class) function names are the same, different parameter list methods are called overloads, they have three characteristics (commonly known as two must be one can) :

Method names must be the same
Parameter list must be different
The return value type can be different
E.g:
public void Sleep ()
{
     Console.WriteLine ("Animal Sleep");
}
public int Sleep (int time)
{
     Console.WriteLine ("Animal {0} point to sleep", time);
     return time;
}

2, override (override): In order to meet their own needs to repeatedly define different implementations of a method, you need to use the override keyword, the method being overridden must be a virtual method, using the virtual keyword. It is characterized by (three identical):

Same method name
Same parameter list
Same return value

For example: the definition in the parent class:
public virtual void EatFood ()
{
     Console.WriteLine ("Animal eating");
}

Definition in subclass:

public override void EatFood ()
{
     Console.WriteLine ("Cat 吃吃");
     //base.EatFood ();
}

Author: