CSharp examples for Custom Type:virtual
A function marked as virtual can be overridden by subclasses to provide a specialized implementation.
Methods, properties, indexers, and events can all be declared virtual.
The signatures, return types, and accessibility of the virtual and overridden methods must be identical.
public class Item { public string Name; public virtual decimal Liability => 0; // Expression-bodied property }
A subclass overrides a virtual method by applying the override modifier:
using System; public class Item { public string Name; public virtual decimal Liability => 0; // Expression-bodied property } public class Stock : Item { public long SharesOwned; } public class Company : Item { public decimal Mortgage; public override decimal Liability => Mortgage; } class Test { static void Main(){ Company mansion = new Company { Name="McMansion", Mortgage=250000 }; Item a = mansion; Console.WriteLine (mansion.Liability); // 250000 Console.WriteLine (a.Liability); // 250000 } }