0

I'm currently working with C#, and I'm using a library where you can public override void but this obviously overrides the entire method.

Is there a keyword for "appending to the method"? For example

    class A  
    {  
        public virtual void HelloWorld()  
        {  
            Console.WriteLine("Do this first");  
            Console.ReadLine();  
        }  
    }  

    class B : A  
    {  
        public append void HelloWorld() // <-- Special "append" keyword?
        {  
            Console.WriteLine("Then do this!");  
            Console.ReadLine();  
        }  
    }  

So that the output of class B : A, HelloWorld() would be

Do this first
Then do this!
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Richard Muthwill
  • 320
  • 2
  • 16

5 Answers5

5

You can call the parent class method by base keyword

class A
{
    public virtual void HelloWorld()
    {
        Console.WriteLine("Do this first");
    }
}

class B : A
{
    public override void HelloWorld() // <-- Special "append" keyword?
    {
        base.HelloWorld();
        Console.WriteLine("Then do this!");
    }
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • Technically this solution is OK, but be careful. Use inheritance only if you truly can my class B "is a" class A. This happens far less than you may think. In other cases you need other solutions, e.g. you may use interfaces, or just call both methods one after another etcetera. You can only inherit from one class, so take care when to use it. – RudolfJan Jun 16 '20 at 07:27
4

You can call base implementation in overridden method via base keyword:

class B : A
{
    public override void HelloWorld() 
    {
        base.HelloWorld(); // will print "Do this first" and wait for console input
        Console.WriteLine("Then do this!");
        Console.ReadLine();
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
2

There is no specific keyword for what you're asking, but you can call the base implementation from the deriving class to achieve the same functionality.

class A  
{  
    public virtual void HelloWorld()  
    {  
        Console.WriteLine("Do this first");  
        Console.ReadLine();  
    }  
}  

class B : A  
{  
    public override void HelloWorld()
    {  
        base.HelloWorld();
        Console.WriteLine("Then do this!");  
        Console.ReadLine();  
    }  
}
Shazi
  • 1,490
  • 1
  • 10
  • 22
1

You need to modify your code to call the base implementation first.

class B : A  
{  
    public override void HelloWorld()
    {
        base.HelloWorld();
        Console.WriteLine("Then do this!");  
        Console.ReadLine();  
    }  
}

Language does not have any concept of this "append", nor does it require you to or provides any way to enforce that a base implementation is always called.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
0

You can use the same work in base and extra work in the child's method

class A
{
    public void DoSomething()
    {
        //Do Something
    }
}

class B: A
{
    public void DoSomethingExtra()
    {
        base.DoSomething();
        //Do extra things
    }
}
AJITH
  • 1,145
  • 7
  • 8
  • It wasn't my downvote, but I can tell you that it comes from the fact that OP is asking about class `A` and `B`'s methods having the **same** names/headers. Your method uses two separate names/headers. – matthew-e-brown Jun 15 '20 at 12:01
  • Ohh,I thought giving a meaningful name would make the context more understandable. – AJITH Jun 15 '20 at 12:26