Using the new keyword when creating a method with the same signature as one in the superclass?

102 views Asked by At

Back to CS101 really quick. I am trying to extend the behavior of the method Add in the class ConcurrentDictionary. As a result of it not being virtual or abstract I do not have to mark it with the override keyword, I can simply create a new function in my subclass with the same signature.

Though when I do this I notice a visual studio message saying

Use the new keyword if hiding was intended.

Does using this keyword change how the method executes when called from an instance of the subclass?

For instance in the code below, if I have an instance of the class MyClassEx and I call Method1 then Method1 in the super class will not be called correct (only if I were to call it myself with base)?. With this same instance if I were to call Method2 the same result would happen, only the subclass's Method2 would execute?

public class MyClass
{
    public void Method1() { Console.Out.WriteLine("MyClass.Method1"); }
    public void Method2() { Console.Out.WriteLine("MyClass.Method2"); }
}

public class MyClassEx : MyClass
{
    public void Method1()
    {
        Console.Out.WriteLine("MyClassEx.Method1");
        //base.Method1();
    }

    public new void Method2()
    {
        Console.Out.WriteLine("MyClassEx.Method2");
        //base.Method2();
    }
}

Is using the new keyword in this manner just "good coding" or does it have some effect?

EDIT: From what it looks like this is just "good coding". Though it raises a slight concern. I wish to create a new Add method in the subclass to change the behavior of the ConcurrentDictionary method. Though from the linked answers it seems that another coder could get around my new implementation by simply referring to the subclass from the super class.

MyClassEx mycex = new MyClassEx();
((MyClass)mycex).Method2();

In this case they could call the superclass's Method2 (the default behavior) ignoring the behavior I have implemented?

2

There are 2 answers

2
mike On BEST ANSWER

Based on this, it is just "good coding". It will suppress the warning that you will get for "hiding" methods, but should not change how the functions work.

0
CarstenP On

Basically, the "new" keyword forces the compiler/runtime to treat the flesh assigned to the value/actor alias as, as the name might suggest: new. Just think of a newly introduced, optional keyword "old" or "inherited" being there to mark the opposite.

Let's assume, class A defines a value alias a with value 42. Let's further assume, class B inherits from class A. Without any keyword, but keeping in mind visibility, B will respond to "c = B.a" with assigning 42 to c. Now, let's assume, class C inherits from class A, too, but overrides the value alias of a with the "new" keyword and defines "a = 666;" within it's constructor. Thirdly, class D inherits from class A, again, and defines "a = 999;".

Now, inherit classes B (BB), C (CC), D (DD), and ask for the values of the a's, respectively. Try!

A "new" thing is a new thing and will not inherit. Use "virtual", "override" and the like to copycat C++ behavior.

But keep in mind that inheriting is usually a bad thing. There's a reason why C# does not support merging inheritance. (B: A; C: A; D: B, C)