Call override function from base class c#

Thamotharan 21 Reputation points

Given the following C# class definitions and code:

    public class Test
        public void name()
        protected virtual void printName()
    public class Test1 : Test
        protected override void printName()
    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?

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,364 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points

    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 additional answer

Sort by: Most helpful
  1. RLWA32 40,941 Reputation points

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

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