Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6356041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:53:16+00:00 2026-05-24T22:53:16+00:00

I have base class( CompareAttribute ) which implemented IClientValidatable [AttributeUsage(AttributeTargets.Property)] public class NotContainsAttribute :

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:53:17+00:00Added an answer on May 24, 2026 at 10:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class with a property which (the get method) I want
I have a base class which contains an abstract/MustInherit method. I want this method
I have a base class in which I want to specify the methods a
I have a base class with a virtual method, and multiple subclasses that override
I have a base class that has the following property: public virtual Dictionary<String, int>
I have a base class which has a method for moving files to appropriate
I have a base class Foo that has an Update() function, which I want
I have a base class which implements the == operator. I want to write
I have base class Foo with method spam and class Bar which overrides spam
I have base class and Execute virtual method. I override execute method in derived

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.