CS0108 'DerivedClass.Method()' hides inherited member 'BaseClass.Method()'. Use the new keyword if hiding was intended

Julio Bello 221 Reputation points
2022-06-20T03:02:37.747+00:00

Hi, Everybody . . .

I have a base class, public class BaseClass { . . . }, and a derived class, public class DerivedClass: BaseClass { . . . }. Each has the same named method, public void Method() { . . . }, and performs similar operations on its own properties. I DO NOT want the derived class to OVERRIDE the base class method. I want both to co-exist and have the derived class's method extend the base class's method. In other words, I want to be able to invoke either class's method (e.g., new BaseClass().Method() or new DerivedClass().Method()) depending on whether I am operating on the base class or the derived class. The derived class invokes the base class method and then performs the same operations on its own properties. See mocked-up sample code below...

namespace NameSpace  
{  
    public class BaseClass  
        public void Method()  
        {  
            // Perform operations on its properties…  
        }  
}  




namespace NameSpace  
{  
    public class DerivedClass : BaseClass  
        public void Method()  
        {  
            base.Method ();  
            // Perform operations on its properties…  
        }  
}  

Consequently, I get the following warning...

CS0108 'DerivedClass.Method()' hides inherited member 'BaseClass.Method()'. Use the new keyword if hiding was intended.

Am I intentionally "hiding" the base class method? Do I want to use the new keyword? Is there an alternate way of accomplishing the same objective without triggering the warning altogether?

Please advise.

Thank-you!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-06-20T06:09:12.573+00:00

    @Julio Bello , Welcome to Microsoft Q&A, If you want to keep both to co-exist, you have to use the new keyword in the DerivedClass Method.

    Am I intentionally "hiding" the base class method?

    Yes, based on the Microsoft doc Compiler Warning (level 2) CS0108, 'member1' hides inherited member 'member2'. Use the new keyword if hiding was intended.

    Do I want to use the new keyword?

    Yes, we need to use the keyword new to solve the warning error, like the following code:

    public class DerivedClass : BaseClass  
        {  
            public new void Method()  
            {  
                base.Method();  
                // Perform operations on its properties…  
            }  
        }  
    

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 56,686 Reputation points
    2022-06-20T16:02:03.607+00:00

    the other option is to declare Method in BaseClass as virtual.

    to decide you must first answer the the question, when instance downcast which to call?

    say you have the method:

    void SomeMethod(BaseClass foo)   
    {   
        foo.Method();   
    }   
    

    and you call:

    var foo = new DerivedClass();   
    SomeMethod(foo);   
       
    

    which implementation of Method do you want called? the BaseClass or the DerivedClass Implementation. use new if you want the BaseClass version, use virtual if you want the DerivedClass version