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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.