question

JulioBello-4525 avatar image
0 Votes"
JulioBello-4525 asked JackJJun-MSFT commented

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

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!




dotnet-csharpdotnet-runtimedotnet-standard
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JulioBello-4525, did you consider my answer to use new keyword to solve the problem?

0 Votes 0 ·
JulioBello-4525 avatar image JulioBello-4525 JackJJun-MSFT ·

Yes... I did... Thank-you!

0 Votes 0 ·
JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered JackJJun-MSFT commented

@JulioBello-4525, 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.




· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yeah this seems like it would solve your problem OP ... I'm always curious when I see scenarios like this. Is there a particular reason that you don't want to override the base method?

0 Votes 0 ·

Thank-you, Jack, for your prompt response. This seems to work for me.

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT JulioBello-4525 ·

@JulioBello-4525, thanks for the feedback, If it works for you, please click "Accept Answer", which will also help others who face the simliar issue.

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Bruce...

Thank-you for your prompt response.

I'm afraid your answer will not work. I clearly indicated: I DO NOT want the derived class to OVERRIDE the base class method. Declaring the Method in BaseClass as virtual will result with it being overridden.

Thank-you, again for your time and effort.

Sincerely,
Julio Bello

0 Votes 0 ·
Bruce-SqlWork avatar image Bruce-SqlWork JulioBello-4525 ·

then use the new rather than override.

0 Votes 0 ·