Base Keyword
In this lesson, you'll get to know the uses of the 'base' keyword in C#.
What Is the base
Keyword?
As you already know, the this
keyword in C# is used to refer to the instance of the current class.
In a similar fashion, the base
keyword in C# is used to refer to the base class members from inside the immediate derived class. The use of base
comes into play when we implement inheritance.
Use Cases of the base
Keyword
The base keyword is used in three major contexts:
Accessing Base Class Fields
The base
keyword can be used to access any non-private fields of the base class directly from the derived class. The base
keyword followed by the dot .
operator is used in the derived class to access a field of the base class.
Consider a field named price
defined inside a Product
class to keep track of the price of a product. Another class named as Beverage
inherits from this Product
class. We declare a field inside the Beverage
class with the same name, price
, but different value. Now if we want to access the price
field of the base class from the derived class, we will then have to use the base
keyword.
Let’s understand this using a bit of code.
Get hands-on with 1400+ tech skills courses.