0

I have base class(CompareAttribute) which implemented IClientValidatable

[AttributeUsage(AttributeTargets.Property)]
public class NotContainsAttribute : CompareAttribute
{
}

I want to override method IEnumerable<ModelClientValidationRule> GetClientValidationRules

but i can't do that because it is not virtual(cannot override inherited member, because it is not marked virtual, abstract, or override).

So i just declare my own method in my NotContainsAttribute class

public new IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
}

After i run the program all working like expected but i get warning in compile time that my class hides inherited member, (Warning "NotContainsAttribute" hides inherited member Use the new keyword if hiding was intended.)

But if I use new keyword

public new IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            yield return new ModelClientValidationNotContains (this.FormatErrorMessage(metadata.GetDisplayName()), CompareAttribute.FormatPropertyForClientValidation(this.OtherProperty));            
            yield break;
        }

In such case my method not used, base class method used instead.

I want my method to be used, without new keyword i get it used but why compiler saying that i need to use new keyword if hiding was intended?

I understand that if hiding was intended means that if you want to hide base method and use your method insteard mark it with new keyword but on practice it is really hiding my method.

May be some one may clarify that and is it good practice to declare method with the same name if that method in base class not allowed to be overridden but really you need to override it?

Joper
  • 8,019
  • 21
  • 53
  • 80

3 Answers3

1

Its just a warning when upon using in the caller method to call this method, you might accidentally call this hiding method which your intention was not to. By providing new scope to it, your giving a new definition rather than masking it.

Warning doesnt mean that your code is imperfect or wrong, its just a heads up for you to not oversee your mistake and bang head in future.

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
1

I don't think your assessment of the situation is correct: If the base class method is not marked as virtual then your method implementation will be hiding it by default.

From MSDN:

If the method in the derived class is not preceded by new or override keywords, the compiler will issue a warning and the method will behave as if the new keyword were present.

You just get a compiler warning because often this is not what the intention of the programmer is and it gives you a chance to correct this common mistake - usually you want to override a base class implementation. It should not make a difference whether or not you mark your method with new - this just makes it explicit that you are in fact hiding the base class method.

Also note that hiding a base class method will not allow this method to be used in a polymorphic fashion: object references typed as the base class that point to an instance of your derived class will use the base class method instead:

CompareAttribute foo = new NotContainsAttribute();
foo.GetClientValidationRules(..) // will call base class method

So your method only will be called if you call the method on an object reference with type NotContainsAttribute :

NotContainsAttribute bar = new NotContainsAttribute();
bar.GetClientValidationRules(..) // will call your method

Also see "Versioning with the Override and New Keywords (C# Programming Guide)" and "Polymorphism, Method Hiding and Overriding in C#" as a reference.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

If a method is not marked as virtual then you cannot change what that method does. Using the new keyword will rarely help you because any code that refers to the base class, rather than your class, will still use the base class's implementation.

Here is an explanation of the difference between new and override: Difference between new and override

Community
  • 1
  • 1
cbp
  • 25,252
  • 29
  • 125
  • 205