C# Base5 Sept 2025 | 6 min read In the C# programming language, the base keyword is utilized to access base class members from within the derived class. It helps to invoke the implementation of the parent class when it is overridden or when we need to call the parent constructor during the object creation. A base class keyword can only be used in an instance method, a constructor, or an instance property accessor. Syntax:It has the following syntax: In this syntax,
C# Base keyword to access the base class fieldIn C# programming, we can use the base keyword to access the fields of the base class inside the derived class. It can be beneficial if base and derived classes contain the same fields. If the derived class does not define the same field, there is no need to use the base keyword. Base class field can be directly accessed by the derived class. C# Base keyword example to access the base class fieldLet's see the simple example of a base keyword in C#, which accesses the field of the base class. ExampleCompile and RunOutput: white black Explanation: In this example, we demonstrate the field hiding wherein Dog possesses its own color field hiding Animal's color. After that, the base.color is used to reach "white" in Animal, whereas color reaches "black" in Dog. C# base keyword to call the base class methodIn C#, we can call the base class method using the base keyword. It is useful if base and derived classes define the same cmethod. In other words, if the method is overridden or if the derived class doesn't define the same method, there is no need to use the base keyword. Base class method can be directly called by the derived class method. C# base keyword example to call the base class methodLet's see the simple example of a base keyword that calls the method of the base class. ExampleCompile and RunOutput: eating... eating bread... Explanation: In this example, we demonstrate the use of the base keyword to call a base class method from an overridden method in the derived class. Here, the Dog class overrides the eat() method, calling first the base Animal method that prints "eating...", followed by its message "eating bread…". C# Base keyword using Constructor ChainingIn the C# programming language, when a class is inherited, the base class constructor is automatically called before the derived class constructor. The base keyword can also be explicitly utilized to call a specific constructor of the base class. C# Base keyword example using constructor chainingLet us take an example to illustrate the base keyword using the constructor chaining in C#. ExampleCompile and RunOutput: animal... dog... Explanation: In this example, we demonstrate the constructor chaining in inheritance where instantiating a Dog object initially invokes the Animal constructor, which prints "animal…", followed by the Dog constructor, which prints "dog…". Constructors Chaining with Parameters in C#In C#, when a base class defines a parameterized constructor, we should explicitly call the derived class to ensure proper initialization. It is accomplished by calling the parameterized base class constructor within the derived class constructor using the base keyword, followed by the respective parameters. It ensures the correct initialization of the base class with the required parameters before the execution of the derived class constructor. C# Constructors Chaining with Parameters ExampleLet us take an example to illustrate the constructor chaining with parameters in C#. ExampleCompile and RunOutput: Animal constructor: Buddy Dog constructor Explanation: In this example, we demonstrate the constructor chaining with parameters wherein initializing Dog("Buddy") makes an initial call to Animal's constructor through base(name) to print "Animal constructor: Buddy", followed by executing the Dog constructor to print "Dog constructor". Difference Between base and this keyword in C#There are several differences between the base and this keyword in C#. Some main differences are as follows: This keyword
Base Keyword
C# Example using base and this keywordLet us take an example to illustrate the base and this keyword in C#. ExampleCompile and RunOutput: Shape Area: 0 Rectangle Area (overridden): 20 Shape Area: 0 This is a Shape This is a Rectangle Explanation: In this example, we have taken a Rectangle class that inherits from Shape and overrides the Area() method while hiding the PrintType() method using new. Inside the ShowInfo() method, both base and this are utilized to demonstrate how the base class methods (base.Area(), base.PrintType()) and the derived class methods (this.PrintType(), overridden Area()) can be accessed separately. ConclusionIn conclusion, the base keyword is an object-oriented programming requirement of C# that enables a derived class to talk to its immediate base class. The "base" keyword is utilized to invoke base class constructors, access base class members and properties, and eliminate problems concerning derived members that can hide base class members. Its usage in the correct context enables cleaner inheritance hierarchies, reuse of code, and reduced confusion in member accesses in complex class hierarchies. C# Base FAQs1) Can we use more than one constructor in a derived class, and how do we select which base constructor to call? Yes, we can have more than one constructor in a derived class; a different base class constructor can be called from each constructor by calling base() with the corresponding required parameters. 2) What happens if the base class lacks a public parameterless constructor? The derived class should explicitly call a base class constructor via the base keyword, selecting among the available parameter signatures. Otherwise, it will not compile. 3) When should the base keyword be employed in methods? Use the base keyword if we wish to call a method or property of the base class that has been overridden or hidden by the derived class. 4) Does C# allow multiple base class inheritance? No, C# does not allow multiple class inheritance. We can inherit from only one base class, but we can inherit from several interfaces. 5) Can a base be applied to fields and properties? Yes, a base can call fields, properties, and methods of the base class that are accessible from the derived class. It is usually applied when the derived class overshades a base class member. Next TopicC# Polymorphism |
We request you to subscribe our newsletter for upcoming updates.