C# Base Keyword

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

 

By using base keyword, we can call a base class method that has been overridden by another method in derived class and we can specify which base class constructor should be called while creating an instance of 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, in case if we try to use base keyword within a static method.

C# Base Keyword Example

Following is the example of using a base keyword to access a 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 above example, we are able to call a base class (Users) method (GetInfo) in derived class overridden method using base keyword and we are able to access properties of base class (Users) in derived class (Details) using base keyword.

 

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

 

C# Base Keyword Example Result

 

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

C# Call Base Class Constructor from Derived Class

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

 

Following is the example of specifying which base class constructor should be called while creating an instance of 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 above example, we are calling base class (BClassconstructors in derived class (DClass) using base keyword.

 

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

 

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

 

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

Topics Covered