.Net inheritance and method overloading

前端 未结 5 911
清酒与你
清酒与你 2021-02-20 11:15

Here is a code sample:

class Program
{
    static void Main(string[] args)
    {
        var obj = new DerivedClass();
        obj.SomeMethod(5);
    }
}

class          


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-20 11:43

    From the C# language reference:

    7.5.5 Function member invocation

    This section describes the process that takes place at run-time to invoke a particular function member. It is assumed that a binding-time process has already determined the particular member to invoke, possibly by applying overload resolution to a set of candidate function members.

    For purposes of describing the invocation process, function members are divided into two categories:

    • Static function members.
    • Instance function members. These are instance methods, instance property accessors, and indexer accessors. Instance function members are either non-virtual or virtual, and are always invoked on a particular instance. The instance is computed by an instance expression, and it becomes accessible within the function member as this (§7.6.7). The run-time processing of a function member invocation consists of the following steps, where M is the function member and, if M is an instance member, E is the instance expression:
      • If M is a static function member:
      • If M is an instance function member declared in a value-type:
      • If M is an instance function member declared in a reference-type:
        • E is evaluated. If this evaluation causes an exception, then no further steps are executed.
        • The argument list is evaluated as described in §7.5.1.
        • If the type of E is a value-type,
        • The value of E is checked to be valid. If the value of E is null, a System.NullReferenceException is thrown and no further steps are executed.
        • The function member implementation to invoke is determined:
          • If the binding-time type of E is an interface,
          • Otherwise, if M is a virtual function member,
          • Otherwise, M is a non-virtual function member, and the function member to invoke is M itself.
        • The function member implementation determined in the step above is invoked. The object referenced by E becomes the object referenced by this.

    What's more in 1.6.6.4 Virtual, override and abstract methods, we have

    When a virtual method is invoked, the run-time type of the instance for which that invocation takes place determines the actual method implementation to invoke. In a nonvirtual method invocation, the compile-time type of the instance is the determining factor.

    So what happens is that when you compile your code, the type of the variable you are using determines what method is called.

    public class A { public void WhoAreYou() { Console.WriteLine("A"); } }
    
    public class B : A  { public void WhoAreYou() { Console.WriteLine("B"); } }
    
    internal class Program
    {
    
    private static void Main(string[] args)
    {
        (new B() as A).WhoAreYou(); // "A"
        (new B()).WhoAreYou(); // "B"
    
        Console.ReadLine();
    }
    

    Please note that the compiler will warn you of a potential problem since the method that will be called differs depending on the type you use to define the class instance.

提交回复
热议问题