Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0135 base keyword

We can use the base keyword to reference members from parent class. It is especially useful when referencing the overridden method. using System; class Shape{ public virtual int Area{ get{ return 0; } } } class Rectangle:Shape{ public int width; public int height; public override int Area{ get{ Console.WriteLine(base.Area); return width * height; } } } class Program { static void Main(string[] args) { Rectangle r = new Rectangle(); r.width = 4; r.height = 6; Console.WriteLine(r.Area); } } The output: 0 24