1

I have an issue on new modifier keyword which is explained below in detail.

public abstract class RangeBase
{
    public event RoutedPropertyChangedEventHandler<int> ValueChanged;
    public int Minimum { get; set; }

    public void OnValueChanged(int oldValue, int newValue)
    {
        ValueChanged(this, new RoutedPropertyChangedEventArgs<int>(1, 2));
    }
}

RangeBase is an abstract class defined a RoutedPropertyChangedEventHandler of type int, a property Minimum of type int and a method OnValueChanged which accepts two int parameters.

public class MyRange : RangeBase
{
    public new event RoutedPropertyChangedEventHandler<double> ValueChanged;

    public new double Minimum { get; set; }

    public new void OnValueChanged(double oldValue, double newValue)
    {
        ValueChanged(this, new RoutedPropertyChangedEventArgs<double>(1, 2));
    }
}

MyRange class derived from RangeBase is also defined a set of fresh members and methods which has same name of Base class members but the type here is double and marked with new modifier keyword. Forget about Generics here.... Now let me explain the real issue.

Members and Properties marked with the new keyword hides the BaseClass Members and Properties of the same name. Here also this works well with me except OnValueChanged method. Intellisense still exposing both Derived and Base class OnValueChanged methods of type int and double. Here I Can hide Minimum property of type int in the BaseClass with the Minimum property of type double in Dervied Class. But it is not working for OnValueChanged method. Could anyone can explain why new keyword is not working in this situation. Thanks in Advance !!!

Vimal CK
  • 3,543
  • 1
  • 26
  • 47
  • 2
    it's overloading, the new keyword is unnecessary. – Yuliam Chandra Jul 19 '14 at 12:53
  • The problem behind the issue: this is just not a good case for inheritance. – H H Jul 19 '14 at 12:57
  • @RohitVats i have already defined a property called Minimum with a different type in the snippet – Vimal CK Jul 19 '14 at 12:57
  • @YuliamChandra I agree this is overloading. But then what new keyword means – Vimal CK Jul 19 '14 at 12:58
  • it simply hides base member when an instance is a derived type and the variable type that holds the instance is also a derived type, check [this post](http://stackoverflow.com/questions/22809604/using-the-new-modifier-in-c-sharp). – Yuliam Chandra Jul 19 '14 at 13:03
  • @YuliamChandra could you please my question once more... – Vimal CK Jul 19 '14 at 14:33
  • 1
    @VimalCK, I already explained, "it simply hides base member when an instance is a derived type and the variable type that holds the instance is also a derived type, check [this post](http://stackoverflow.com/questions/22809604/using-the-new-modifier-in-c-sharp)" – Yuliam Chandra Jul 19 '14 at 14:37
  • @YuliamChandra I know why new keyword is using and what is overloading and overriding.. The question is if i can shadow a Property with a same name of different type then why it is not working for a method with same name with same no.of parameters but the parameter type is different. – Vimal CK Jul 19 '14 at 17:00

1 Answers1

3

You can't hide a method which differs in signature, it will be treated as method overloading instead of method hiding.

  • A constant, field, property, event, or type introduced in a class or struct hides all base class members with the same name.

  • A method introduced in a class or struct hides all non-method base class members with the same name, and all base class methods with the same signature (method name and parameter count, modifiers, and types).

  • An indexer introduced in a class or struct hides all base class indexers with the same signature (parameter count and types).

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Well, I've got the same problem and my signature is the same. Have a look: https://dotnetfiddle.net/vYz2we Intellisence, when typing `.Test(` shows both SomeService method and SomeAbstractService method too. – pro100tom Jan 13 '22 at 10:47