Страницы

воскресенье, 14 февраля 2016 г.

Method override in C#

Hi, everyone! Recently, I decided to put all basic info about method overriding in one place. Keeping in mind that method, which is overriden may be virtual or not. Or you may just want to make name hiding (don't know why anybody may want to do that). So, here we go.

Non-virtual methods

class A
{
    void Foo() {}
}

class B: A
{
    void Foo() {} // compiler warning B.Foo() hides A.Foo()
}
To resolve warning:
class B: A
{
    new void Foo() {} // explicitly say to compiler, that hiding is intended using "new" keyword
}