wasAsked.

Any other way to implement overriding other than using 'virtual' and 'override'

HCA Healthcare · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

True runtime overriding requires a virtual, abstract, or already-overridden base member plus override in the derived class. Interfaces provide runtime polymorphism without overriding a class member. The new keyword performs hiding, not overriding. Example: public interface IPriceRule { decimal Apply(decimal value); } public sealed class TaxRule : IPriceRule { public decimal Apply(decimal value) => value * 1.18m; } IPriceRule rule = new TaxRule(); Use interfaces or composition when types share a capability rather than a genuine is-a relationship. Avoid presenting method hiding as overriding because dispatch can depend on the reference's compile-time type and surprise callers.

Backbencher Developer · · 0 likes

Sign in to add your answer

Related interview topics