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 instance property accessor, and it will throw an error if we try to use the base keyword within a static method.
Following is the example of using a base keyword to access base class properties and the method that has been overridden by the derived class in the c# programming language.
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 below.
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.
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.
If you observe the above example, we call base class (BClass) constructors in a derived class (DClass) using the base keyword.
When you execute the above c# program, you will get the result below.
This is how you can use the base keyword in c# to access base class members, such as methods, properties, etc., in the derived class based on our requirements.