S.METHOD.NEW
Avoid use of new keyword for hiding methods
This rule triggers on each use of the "new" key word when it is used to hide a method in the base class. C# lets you redefine a method in the base class using the keyword "new". However, this should be used with great caution since hiding a method from the base class can create confusion for the users of the class. Limitation: The rule does not check static member methods.
Vulnerability and risk
Avoids confusion for the user of the class.
Mitigation and prevention
Effective C# by Bill Wagner (Item 29) has more details on this. It recommends that the usage of "new" to override a method be restricted to the situation where the base class has changed and introduced a method that is already defined in the derived class.
Repair for this usually requires thinking about the design and making if possible avoiding the use of the keyword "new". If it is not possible to avoid, providing a warning in a prominent fashion may help.
Vulnerable code example
public class BaseClass
{
public int GetSomeValue()
{
return 5;
}
}
public class DerivedClass : BaseClass
{
public new int GetSomeValue() // Violation
{
return 8;
}
public static void Main()
{
BaseClass a = new DerivedClass();
DerivedClass b = a as DerivedClass;
// res1 and res2 get different values below.
// This will surprise programs that cannot see the definition
// of the classes. They expect both these to return the same value
// (the situation is worse if these have side effects)
int res1 = a.GetSomeValue();
int res2 = b.GetSomeValue();
}
}