C# Method Overloading and Method Overriding with Examples

  By : Suresh Dasari
  Posted On : 10-May-2023

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.

What is Method Overloading in C#?

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.

C# Method Overloading with Different Number of Parameters Example

Following is the example of implementing the method overloading in c# using a different number of arguments.

 

using System;

namespace Tutlane
{
   public class MathOperations
   {
      public void Addition(int a, int b)
      {
         Console.WriteLine("a + b = {0}", a + b);
      }
      public void Addition(int a, int b, int c)
      {
         Console.WriteLine("a + b + c = {0}", a + b + c);
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         MathOperations operations = new MathOperations();
         operations.Addition(10, 15);
         operations.Addition(10, 15, 20);
         Console.ReadLine();
      }
   }
}

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.

 

C# Method Overloading with Different Number of Arguments Example Result

C# Method Overloading with Different Type of Parameters

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.

 

using System;

namespace Tutlane
{
   public class UserInfo
   {
      public string GetDetails(string a)
      {
         return $"Name: {a}";
      }
      public string GetDetails(int a)
      {
         return $"Age: {a}";
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         UserInfo user = new UserInfo();
         var name = user.GetDetails("Suresh Dasari");
         var age = user.GetDetails(35);
         Console.WriteLine(name);
         Console.WriteLine(age);
         Console.ReadLine();
      }
   }
}

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.

 

C# Method Overloading with Different Type of Parameters Example Result

C# Method Overloading with Different Order of Parameters

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.

 

using System;

namespace Tutlane
{
   public class UserInfo
   {
      public string GetDetails(string a, int b)
      {
         return $"Name: {a}, Age: {b}";
      }
      public string GetDetails(int a, string b)
      {
         return $"Location: {a}, Pincode: {b}";
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         UserInfo user = new UserInfo();
         var result1 = user.GetDetails("Suresh Dasari", 35);
         var result2 = user.GetDetails(522309, "Guntur");
         Console.WriteLine(result1);
         Console.WriteLine(result2);
         Console.ReadLine();
      }
   }
}

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.

 

C# Method Overloading with Different Order of Parameters Example Result

 

To learn more about method overloading, visit Method Overloading in C# with Examples.

What is Method Overriding in C#?

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.

C# Method Overriding Example

Following is the example of implementing the method overriding in c# using virtual and override keywords.

 

using System;

namespace Tutlane
{
   // Base Class
   public class User
   {
      public virtual void GetDetails()
      {
         Console.WriteLine("Base class method");
      }
   }
   // Derived Class
   public class Details : User
   {
      public override void GetDetails()
      {
         Console.WriteLine("Derived class method");
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         Details userDetails = new Details();
         userDetails.GetDetails();
         Console.ReadLine();
      }
   }
}

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.

 

C# Method Overriding Example Result

 

To learn more about method overriding, visit Method Overriding in C# with Examples.

Differences between Method Overloading and Method Overriding in C#

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 OverloadingMethod 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.