Confused about “override” vs. “new” in C#

前端 未结 4 1064
粉色の甜心
粉色の甜心 2020-12-09 16:03

I\'m having the following classes:

class Base
{
    public virtual void Print()
    {
        Console.WriteLine(\"Base\");
    }
}

class Der1 : Base
{
    p         


        
4条回答
  •  感动是毒
    2020-12-09 16:25

    It's because Der1 does not override Print, it replaces it with a brand new method that happens to have the same name (this is caused by the use of the new keyword). So, when the object is cast to Base it calls Print in Base; there is no override to call..

提交回复
热议问题