In c#, the override keyword is used to override a base class virtual members such as properties, methods, etc., in the derived class to modify it based on our requirements.
Following is a simple example of defining a method with an override keyword in c# programming language.
If you observe the above code snippet, we defined a GetInfo method with a virtual keyword in the base class (Users). The GetInfo method can be overridden by any derived class that inherits properties from the base class using the override keyword.
In c#, we cannot override non-virtual or static methods. If you want to override a method, you need to define it with a virtual keyword.
To override a base class method in the derived class, both the override and virtual methods must have the same signatures and access modifiers.
In c#, we should not use static, new, or virtual modifiers to modify an override method. The overriding property declaration must specify the same access modifier, type, and name as inherited property.
Following is the example of using an override keyword to override virtual members of the base class in c# programming language.
If you observe the above example, we are overriding a base class (BClass) methods and properties which we defined with the virtual keyword in a derived class (DClass) using the override keyword.
When you execute the above c# program, you will get the result below.
This is how you can use the override keyword in the derived class to override a base class virtual members such as methods, properties, etc., in c# based on our requirements.