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! 
4
  • and how do you want to call the method? Commented Jun 15, 2020 at 11:52
  • 3
    Normally, you'd just call the base implementation from the override. Did this not work out for you? Commented Jun 15, 2020 at 11:53
  • There is no such thing in C# or any programming language AFAIK. It is pointless to perform this append when you can override while still performing the base in OOP. Commented Jun 15, 2020 at 11:54
  • 1
    @spender well that's what I learned today ;) Commented Jun 15, 2020 at 12:26

5 Answers 5

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!"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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(); } } 

2 Comments

Please code this so that it compiles without warning.
@Enigmativity thank you, missed for some reason that.
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(); } } 

Comments

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.

Comments

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

2 Comments

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.
Ohh,I thought giving a meaningful name would make the context more understandable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.