8

I have two classes

class  A
{
     public virtual void Metod1()
     {
         Console.WriteLine("A.Method1");
     }
}

class B : A
{
    public new void  Metod1()
    {
        Console.WriteLine("B.Method1");
    }
}

Then I write some code that declares instances of these classes, and calls their methods

     static void Main(string[] args)
    {
        A a = new B();
        a.Metod1();
        B b = new B();
        b.Metod1();
        Console.ReadKey();
    }

I gave following result:

A.Method1
B.Method1

But when I delete keyword new from signature method1 of class B and run Main method I got same result. Question:Is new keyword in signature of methods only for better readability?

Edit:Are there cases where we can not do without a new keyword?

vborutenko
  • 4,323
  • 5
  • 28
  • 48
  • you have to use override or new in class B bacause otherwise it hides the parent element's method.. default is new so if you won't place new it gives you no error and run fine.. – Vishal Sharma Apr 04 '13 at 06:44
  • Side note: Consider "confuse readers and users of the code" to be main usage of "new" in such context. There are cases when using it may improve readability (i.e. need to derive class for some unfriendly base class), but in most cases it only make harder to understand when method of base class will be called and when derived one. – Alexei Levenkov Apr 04 '13 at 06:52

3 Answers3

8

The new keyword let the code reader be aware that this method will hide the method with the same name in its base. Even if you ommited it, the compiler would treat it in the same way, but warn you about that.

Since you declared the method as virtual, you would get this warning when you compile:

'B.Metod1()' hides inherited member 'A.Metod1()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

However, if you change the declaration by removing virtual as:

  • Declaration without virtual in A

    class A {
        public void Metod1() {
            Console.WriteLine("A.Method1");
        }
    }
    
    class B: A {
        public void Metod1() {
            Console.WriteLine("B.Method1");
        }
    }
    

Will still compile, but the warning message would be:

'B.Metod1()' hides inherited member 'A.Metod1()'. Use the new keyword if hiding was intended.

In either way as above, the following:

  • Test code

    var b=new B();
    b.Metod1();
    (b as A).Metod1();
    

will output:

B.Method1
A.Method1

But, if you declare as the following:

  • Declaration with override in B

    class A {
        public virtual void Metod1() {
            Console.WriteLine("A.Method1");
        }
    }
    
    class B: A {
        public override void Metod1() {
            Console.WriteLine("B.Method1");
        }
    }
    

Then the output would be:

B.Method1
B.Method1

That is because Method1 of the base class is not just hidden but overridden by the derived class.

A case that you cannot just use new is that if Method1 is a abstract method in a abstract class:

  • Code that must use override in derived classes

    abstract class A {
        public abstract void Metod1();
    }
    
    class B: A {
        public override void Metod1() { // use `new` instead of `override` would not compile
            Console.WriteLine("B.Method1");
        }
    }
    

In this case you cannot just use new, because A requires derived classes to override Method1.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
  • It's ok,but question:Is new keyword in signature of methods only for better readability and Are there cases where we can not do without a new keyword – vborutenko Apr 04 '13 at 06:47
  • so it doesn't actually change any behavior -- it's just for readability's sake?? – Don Cheadle Jun 22 '15 at 22:12
6

Knowing When to Use Override and New Keywords (C# Programming Guide)

In C#, a method in a derived class can have the same name as a method in the base class. You can specify how the methods interact by using the new and override keywords. The override modifier extends the base class method, and the new modifier hides it.

also

By using new, you are asserting that you are aware that the member that it modifies hides a member that is inherited from the base class.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • does base class still need `virtual` in method for `new` in derived to have any affect? In my tests, having `new` on a derived class has same behavior as simply not having `override` - what am I missing? What is meant by `hides it`? – Don Cheadle Jun 22 '15 at 22:11
3

Yes, you are correct, it is for better readability in this situation.

But check carefully you should have a warning for not using new when hiding the base method.

You can read more here: http://msdn.microsoft.com/en-us/library/vstudio/435f1dw2.aspx

G.Y
  • 6,042
  • 2
  • 37
  • 54
  • You wrote it is for better readability in this situation.But Are there situations where we can not do without a new keyword – vborutenko Apr 04 '13 at 06:44
  • @cosset Yes,when it is not used as a modifier, I.e: Class1 C = new Class1() or when you set to treat warnings as errors. – G.Y Apr 04 '13 at 06:59
  • Are there situations where we can not do without a new keyword in signature of methods,fields – vborutenko Apr 04 '13 at 07:00
  • @cosset New keyword can be used either as operator, modifier and constraint in a generic declaration - only when it is used as modifier it can be omitted with the price of a warning, in the other situations it can not be omitted without changing the code behavior. – G.Y Apr 04 '13 at 07:07