C# Base Keyword

In c#, the base keyword is useful to access base class members such as properties, methods, etc., in the derived class.

 

Using the base keyword, we can call a base class method that has been overridden by another method in the derived class, and we can specify which base class constructor should be called while creating an instance of the derived class.

 

In c#, the base keyword is permitted to use only within an instance method, constructor, or in an instance property accessor, and it will throw an error if we try to use the base keyword within a static method.

C# Base Keyword Example

Following is the example of using a base keyword to access base class properties and the method that has been overridden by derived class in c# programming language.

 

using System;

namespace Tutlane
{
    // Base Class
    public class Users
    {
       public string name = "Suresh Dasari";
       public string location = "Hyderabad";
       public int age = 32;
       public virtual void GetInfo()
       {
          Console.WriteLine("Name: {0}", name);
          Console.WriteLine("Location: {0}", location);
       }
    }
    // Derived Class
    public class Details : Users
    {
        public override void GetInfo()
        {
          base.GetInfo();
          Console.WriteLine("Age: {0}", base.age);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          Details d = new Details();
          d.GetInfo();
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we are able to call a base class (Users) method (GetInfo) in the derived class overridden method using the base keyword, and we are able to access properties of the base class (Users) in a derived class (Details) using base keyword.

 

When you execute the above c# program, you will get the result as shown below.

 

C# Base Keyword Example Result

 

Using the base keyword, we can directly access base class fields in the derived class, but the base class methods can be accessed only in overridden methods of the derived class using the base keyword.

C# Call Base Class Constructor from Derived Class

As discussed, we can call a base class constructor from the derived class using the base keyword.

 

Following is the example of specifying which base class constructor should be called while creating an instance of the derived class.

 

using System;

namespace Tutlane
{
    // Base Class
    public class BClass
    {
       public BClass()
       {
          Console.WriteLine("Welcome to Tutlane");
       }
       public BClass(string a, string b)
       {
          Console.WriteLine("Name: {0}", a);
          Console.WriteLine("Location: {0}", b);
       }
    }
    // Derived Class
    public class DClass : BClass
    {
        // This constructor will call default constructor
        public DClass(): base()
        {
        }

       // This constructor will call parameterized constructor
       public DClass(string x, string y): base(x, y)
       {
       }
    }
    class Program
    {
        static void Main(string[] args)
        {
           DClass d = new DClass();
           DClass d1 = new DClass("Suresh Dasari", "Hyderabad");
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
        }
    }
}

If you observe the above example, we call base class (BClassconstructors in a derived class (DClass) using the base keyword.

 

When you execute the above c# program, you will get the result as shown below.

 

C# Call a Base Class Constructor in Derived Class Example Result

 

This is how you can use the base keyword in c# to access base class members, such as methodsproperties, etc., in derived class based on our requirements.