C# virtual methods question

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


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


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.


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


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.


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.


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".


  • Securing web server against MITM attack in Safari
  • Passing parameters in the Form constructor, winforms c#
  • nginx, upstream, cors fail
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • help('modules') crashing? Not sure how to fix
  • Can XOR be expressed using SKI combinators?
  • Converter from SAT to 3-SAT
  • Excel VBA How to populate a multi-dimensional (3d) array with values from multiple excel ranges?
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • abstracting over a collection
  • Jquery Knockout: ko.computed() vs classic function?
  • Can't remove headers after they are sent
  • Azure table store snapshot/backup capability
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • How to get current document uri in XSLT?
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • SharedPreferences or SQLite Database?
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • How to define and use opencv mat of user type
  • Asynchronous UI Testing in Xcode With Swift
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • Why is an OPTIONS request sent to the server?
  • Azure Cloud Service Web Role web pages do not load
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • PHP: When would you need the self:: keyword?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy
  • Does armcc optimizes non-volatile variables with -O0?
  • Busy indicator not showing up in wpf window [duplicate]
  • Binding checkboxes to object values in AngularJs
  • Why do underscore prefixed variables exist?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?