What is the possibility in C # to force a call to a base class function in a subclass function?

Example:

class A { public virtual void F(){...} } class B : A { public override void F() { base.F();//<--- Π΄ΠΎΠ»ΠΆΠ½Π° Π±Ρ‹Ρ‚ΡŒ эта строка! Π˜Π½Π°Ρ‡Π΅ , //Π²Ρ‹ΠΊΠΈΠ½ΡƒΡ‚ΡŒ ΠΎΡˆΠΈΠ±ΠΊΡƒ, ΠΊ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Ρƒ. ... } } 

Thank!

  • one
    No, there is no native way to force a method to call the base class. You can come up with some kind of flag trick ... Why do you need this? Describe the task in more detail - rdorn
  • See the Pattern Method design pattern. - Alexander Petrov

2 answers 2

You can do this: the order of the call is determined in the parent, and the heirs are obliged to override their part. If this part is optional, then you can relax the condition, and make the method virtual instead of abstract .

 class A { public void F() { // Π±Π°Π·ΠΎΠ²Ρ‹ΠΉ ΠΊΠΎΠ΄ FInternal(); } protected abstract void FInternal(); // Π±ΠΎΠ»Π΅Π΅ мягкий Π²Π°Ρ€ΠΈΠ°Π½Ρ‚ //protected virtual void FInternal() //{ //} } class B : A { protected override void FInternal() { ... } } 
  • Ahead for a minute :) - VladD

Inheritance you will not achieve. Make a service from the base class to which you transfer the client class so that it is under service control:

 interface IClient() { void F(); } class Service : IClient { public void Execute(IClient c) { F(); cF(); } public void F() { ... } }; 
  • one
    That is, in essence, composition instead of inheritance? A good idea. - VladD