C# base Keyword

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;/*from   w  w w  . ja v a 2  s.  com*/

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.ja  va  2 s .c  om

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  w  w  . ja v a 2s  . 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; /*from  w w  w. java 2 s  .  c  om*/
 
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 ww  . ja 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.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor