C# base Keyword

In this chapter you will learn:

  1. What is base keyword
  2. Using base to Access a Hidden Name
  3. Example for C# base Keyword
  4. Call constructor in base class
  5. A demo showing what is name hiding
  6. How to use base to reference parent class
  7. How to call a hiddel method from parent class

Description

The base keyword is similar to the this keyword. It serves two essential purposes:

Accessing an overridden function member from the subclass Calling a base class constructor

Syntax

'base' refers to the base class of the derived class in which it is used. This usage has the following general form:

base.member

Example

The following code uses base keyword to access field in the base class.


using System;//  w  w  w  . ja v a 2s.  c  om

class BaseClass                                      
{
   public string Field1 = "In the base class";
}

class DerivedClass : BaseClass                        
{
   new public string Field1 = "In the derived class";

   public void Display()
   {
      Console.WriteLine("{0}", Field1);              // Access the derived class.
      Console.WriteLine("{0}", base.Field1);         // Access the base class.
   }
}

class Program
{
   static void Main()
   {
      DerivedClass oc = new DerivedClass();
      oc.Display();
   }
}

The code above generates the following result.

Example 2

The following code shows how to call constructor from base class with base keyword.


using System;/*  w w  w.  j  a  v a2 s  .  com*/

public class BaseClass
{
    public BaseClass(int x)
    {
        this.x = x;
    }
    public int X
    {
        get
        {
            return(x);
        }
    }
    int    x;
}

public class Derived: BaseClass
{
    public Derived(int x): base(x)
    {
    }
}

class MainClass
{
    public static void Main()
    {
        Derived d = new Derived(15);
        Console.WriteLine("X = {0}", d.X);
    }
}

The code above generates the following result.

What is name hiding

An example of inheritance-related name hiding.


using System; //from w ww . j  ava 2 s  .c  o m
 
class BaseClass { 
  public int i = 0; 
} 
 
// Create a derived class. 
class DerivedClass : BaseClass { 
  new int i; // this i hides the i in BaseClass 
 
  public DerivedClass(int b) { 
    i = b; // i in DerivedClass 
  } 
 
  public void show() { 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    DerivedClass ob = new DerivedClass(2); 
 
    ob.show(); 
  } 
}

The code above generates the following result.

Example 3

The following code shows to how to use base to overcome name hiding.


using System; // w w w.ja v a 2  s.c  o m
 
class BaseClass { 
  public int i = 0; 
} 
 
// Create a derived class. 
class DerivedClass : BaseClass { 
  new int i; // this i hides the i in BaseClass 
 
  public DerivedClass(int a, int b) { 
    base.i = a; // this uncovers the i in BaseClass 
    i = b; // i in DerivedClass 
  } 
 
  public void show() { 
    // this displays the i in BaseClass. 
    Console.WriteLine("i in base class: " + base.i); 
 
    // this displays the i in DerivedClass 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    DerivedClass ob = new DerivedClass(1, 2); 
 
    ob.show(); 
  } 
}

The code above generates the following result.

Example 4

How to call a hiddel method from parent class


using System; /*  w w  w  .j a  v a2  s .c  o  m*/
 
class BaseClass { 
  public int i = 0; 
 
  // show() in BaseClass 
  public void show() { 
    Console.WriteLine("i in base class: " + i); 
  } 
} 
 
// Create a derived class. 
class DerivedClass : BaseClass { 
  new int i; // this i hides the i in BaseClass 
 
  public DerivedClass(int a, int b) { 
    base.i = a; // this uncovers the i in BaseClass 
    i = b; // i in DerivedClass 
  } 

  // This hides show() in BaseClass. Notice the use of new. 
  new public void show() { 
    base.show(); // this calls show() in BaseClass 
 
    // this displays the i in DerivedClass 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    DerivedClass ob = new DerivedClass(1, 2); 
 
    ob.show(); 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What
  2. Example for C# seal Functions and Classes
Home »
  C# Tutorial »
    C# Types »
      C# Inheritance
C# Class Inheritance
C# base Keyword
C# seal Functions and Classes
C# Abstract Classes and Abstract Members
C# Polymorphism
C# Virtual Function Members
C# Override methods
C# Shadow inherited members
C# as operator
C# is operator