question

Thamotharan-5143 avatar image
0 Votes"
Thamotharan-5143 asked Thamotharan-5143 commented

Call override function from base class c#

Given the following C# class definitions and code:

     public class Test
     {
         public void name()
         {
             printName();
         }
         protected virtual void printName()
         {
             Console.WriteLine("name");
         }
     }
     public class Test1 : Test
     {
         protected override void printName()
         {
             Console.WriteLine("name1");
         }
     }
     class Program
     {
         static void Main(string[] args)
         {
             new Test().name();
         }
     }

Output is "name"
But I am excepting output is "name1"
How can I do it?

dotnet-csharp
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.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered Thamotharan-5143 commented

Hi Thamotharan-5143,
You cannot explicitly call the base function from outside the scope of Test or Test1.
And Sasha Truf has pointed out that you can edit MSIL instead of C# to achieve it.
More details you can refer to this thread.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

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
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.

Thank you got it.

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered Thamotharan-5143 commented

If I correctly understand what you want to do the following should meet your objective.

 Test t = new Test();
 t.name(); // Output is "name"
    
 Test tBase = new Test1();
 tBase.name(); // Output is "name1"

· 1
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.

Yes got it thank you

0 Votes 0 ·