.Net inheritance and method overloading

前端 未结 5 1007
清酒与你
清酒与你 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:55

    var obj = new DerivedClass();
    

    var keyword is just syntactic sugar in C#; this is essentially the same as:

    DerivedClass obj = new DerivedClass();
    

    hence you're calling DerrivedClass.SomeMethod, which is just the behaviour you're experiencing. You would see a difference if you defined your variable like this:

    BaseClass obj = new DerivedClass();
    

提交回复
热议问题