One of my favorite interview questions is what the difference is between a virtual and a non-virtual method. If you were learning development in the early 90s, you heard about a relatively spirited controversy regarding the use of virtual functions. The proponents shared the programming flexibility that it afforded. The detractors warned of the performance implications. Luckily, Moore’s law has silenced the detractors. The impact of the extra lookups that must be done for virtual functions has long been decided as trivial.
Relatively few folks are aware what the impacts of making methods virtual are, and of those who fully understand virtual methods only a few really understand how they impact the use of interfaces in .NET. This article is designed to walk through the results you can expect when utilizing both non-virtual and virtual methods via an interface in .NET.
Before explaining virtual methods, it’s important to review one of the fundamental concepts to object-oriented programming. Any class that is derived from a base class can be used anywhere the base class is expected. This means that if I have a class—SubB—which derives (or is based on) BaseA and I have methods that expect an instance of BaseA, I can pass in an instance of SubB instead.