Saturday, December 31, 2022

This is the first post

That's a simultaneously fun and annoying problem to have! :) I believe that one of the missing links here is this:

  • The override modifier is related to virtual or abstract methods; a method with an override modifier simply provides an alternative implementation to an existing virtual or abstract method. A virtual method has a default implementation, an abstract method has no implementation at all.
  • The new modifier however can be applied to any method and simply allows the reuse of a name that was already taken. These methods are not related to each other at all, the only similarity being that they forcibly share the same name.

The problem with new is that the type using a new method "pretends" that the original implementation never existed. The base class however is absolutely unaware of this - new severs the link in the hierarchy, and this is what's casing your problem.

Generally speaking, you do not want to use the new modifier.

That, and you probably wanted to use var m = new Program(); instead - reasons being explained below.

No comments: