Here, we will learn what is method overloading in c#, what is method overriding in c#, and the differences between method overloading and overriding in c# with examples.
In c#, method overloading is a process of defining multiple methods (two or more) with same name but with a different number of parameters, different types of parameters, or different order of the parameters in the same class. The method overloading is useful to perform multiple tasks with same method name by passing different arguments.
Now, we will learn how to implement method overloading using different number of parameters, different type of parameters, and different order of parameters with examples.
Following is the example of implementing the method overloading in c# using a different number of arguments.
In this example, we created two methods with same Addition name but with a different number of parameters. When we call Addition with 2 parameters the first method will be executed. When we call the Addition method with 3 parameters, the second method will be executed.
When you execute this c# method overloading example, you will get the result as shown below.
We can also achieve method overloading by using different type of parameters. Following is the example of implementing the method overloading using different type of parameters.
In this example, we created two methods with same GetDetails name but with different type of parameters. When we call the GetDetails method with string parameter type, it will execute the first method. When we call the GetDetails method with an integer parameter type, it will execute the second method.
When you execute this c# method overloading example, you will get the result as shown below.
Like previous different number of parameters and different type of parameters examples, we can achieve method overloading with different order of parameters.
Following is the example of implementing the method overloading with different order of parameters.
In this example, we created two methods with same GetDetails name but with different order of parameters.
When you execute this method overloading example, you will get the result as shown below.
To learn more about method overloading, visit Method Overloading in C# with Examples.
In c#, method overriding means overriding a base class method in a derived class by creating a method with same name and signature (return type and parameters) to perform a different task. The method overriding will help us to change the behavior of the base class method in the derived class.
To implement method overriding in c#, you need to use virtual
and override
keywords. The base class method that you want to override in the derived class needs to be defined with a virtual
keyword. In the derived class, you need to use the override
keyword while defining the method with same name and parameters.
Following is the example of implementing the method overriding in c# using virtual
and override
keywords.
In this example, we defined a base class User with the virtual method GetDetails to allow derived classes to override and provide their own implementation. In the derived class Details, we override the GetDetails method by using the override keyword and provided our own implementation. When we call the GetDetails method using the derived class Details object, it will execute the derived class method.
When you execute this c# method overriding example, you will get the result as shown below.
To learn more about method overriding, visit Method Overriding in C# with Examples.
As discussed, the method overloading and method overriding in c# are useful to create the methods with same name but with different functionality.
The following table lists the differences between method overloading and overriding in c# with examples.
Method Overloading | Method Overriding |
---|---|
Method overloading means defining multiple methods with same name but with different parameters. | Method overriding means overriding base class method in the derived class by creating a method with same signature (name, parameters, and return type). |
Method overloading can be implemented within a single class. | Method overriding can be implemented in a derived class that inherits from a base class. |
It is useful to perform different tasks with same method name by passing different parameters. | It is useful to change the behavior of the base class method in the derived class. |
Based on the arguments that we pass to the method, the compiler will determine which method to invoke at compile time. | Based on the object type, the compiler will determine which method to invoke at runtime. |
The return type of overloaded methods can be different. | The signature of the overridden method in the derived class must be same as the base method. |
To learn more about method overloading, method overriding, and other c# topics, visit our C# Tutorial.