2

There is a thing I do not understand well: when virtual method is called, the base method is called as well?

Because when I use public override WinForm OnPaint method, in its body base.OnPaint(e) is called. I do not understand it, I thought virtual methods overrides the original one. If it is not usually called, why it is called in this case? Thank you

Snake
  • 53
  • 4
  • For when you do want to ensure that the base method is called by an override, see http://stackoverflow.com/questions/2872137/does-this-keyword-exist-when-an-overriding-method-needs-to-call-the-parent/2872190#2872190 or the 'template method' http://en.wikipedia.org/wiki/Template_method_pattern or 'non-virtual interface' http://en.csharp-online.net/CSharp_Canonical_Forms%E2%80%94NVI_Pattern patterns – Pete Kirkham Jul 07 '10 at 08:53

5 Answers5

11

No, it is not called, if you don't call it explicitly from the derived class.

onof
  • 17,167
  • 7
  • 49
  • 85
5

when virtual method is called, the base method is called as well?

No.

Because when I use public override OnPaint(), in its body base.OnPaint(e) is called. I do not understand it, I thought virtual methods overrides the original one.

The developer chose to call base.OnPaint(e) and also do something else. In other words, the base implementation is something which you have at your hand in case it is useful to you - you can call it, and then do some additional specific work.

If the function body was only a call to the base, then it would be equivalent to not writing the overriding function at all.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • Hmm..its standard WinForm OnPaint method. Is it called because the base contains some low level graphical routines that actually draw? – Snake Jul 07 '10 at 08:20
  • @Snake: Your comment is very specific to the implementation of one function. I don't know why the base is always called but I can guess that there is something common that always needs to be done. – Daniel Daranas Jul 07 '10 at 08:23
  • For an OnPaint method, it is likely that your code is drawing something "on top" of the default graphical representation. That's why the base implementation of OnPaint may be call: to draw the background, etc. – Xavier Poinas Jul 07 '10 at 09:07
1

I understand that, when you say "virtual method is called", you mean overriding method.

The answer is no. Whenever any version of the virtual method is called, it does not automatically call its base class methods. But sometimes you want to call them, so you include an explicit call to the base class method. Something like this:

class Base
{
  public virtual void VirtualMethod()
  {
    // Insert code here
  }
}

class Derived : Base
{
  public override void VirtualMethod()
  {
    // Insert code here
    base.VirtualMethod(); // Explicit call to the base class method
  }
}

If we had not included the instruction base.VirtualMethod();, the base class method would not be called when the derived class method is invoked.

Gorpik
  • 10,940
  • 4
  • 36
  • 56
0

when virtual method is called, the base method is called as well?

This depends whether you call base.XXX in the overridden method or not.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

Sometimes you have code in the "base" method that is useful no matter which child method is being executed, then the children can just call the base method, but that's optional.

Unless you specifically call the base method, the child method gets called instead of the base rather than "as well as".

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116