Method Overloading and Method Overriding in C#

Method Overloading and Method Overriding in C#

09 Jun 2025
Advanced
22.8K Views
27 min read
Learn with an interactive course and practical hands-on labs

Best Free C# Course: Learn C# In 21 Days

Method Overloading and Method Overriding in C# are two useful features that make coding easier and better. Method Overloading in C# means using the same method name in the same class but with different inputs, so it can do different things. Method overriding in C# means using the same method name in the child class to change how it works from the parent class. Both Method Overloading and Method Overriding in C# help write clean, flexible, and easy-to-read code.

In this C# tutorial, you’ll learn the simple difference between Method Overloading and Method Overriding in C# with easy examples, a must-know for writing smart and clean C# code.

To gain a deeper understanding of these concepts and other C# techniques, be sure to enroll in our Free C # Course for a comprehensive learning experience.

Method Overloading in C#

Method overloading in C# allows the definition of multiple methods with the same name in the same class but with different parameters. These methods perform similar tasks but can accept different types or numbers of parameters, enabling flexibility and enhancing code readability and reusability.

Why Do We Need Method Overloading in C#?

We need Method Overloading in C# for several reasons:

  • Method overloading lets you have multiple methods with the same name but different parameters in one class.
  • It makes code easier to read by using one name for similar tasks.
  • It allows working with different data types without new method names.
  • It keeps code organized by grouping related functions under one name.
  • It uses compile-time polymorphism to choose the right method, making the program efficient.

Important Points to Remember in Method Overloading

  • Overloaded methods must have different types or numbers of parameters so the compiler can tell them apart.
  • You cannot overload methods by only changing the return type because C# does not use return type to distinguish methods.
  • Overloaded methods can have different access modifiers like public or private.
  • Both static and instance methods can be overloaded.
  • Methods can differ by the data types of their parameters (for example, one takes an int, another a string).
  • Methods can differ by the number of parameters they accept.
  • All overloaded methods must have the same name within the same class.

Read More - C # Interview Questions

Different ways of doing the overloading methods

Method overloading can be done by changing:

  1. The number of parameters in the two methods.
  2. The data types of the parameters of the methods.
  3. The Order of the parameters of methods.

Example 1. The number of parameters in the two methods

 // C# program to demonstrate the function overloading by changing the Number of parameters
using System;
class DNT {
// adding two integer values.
public int Add(int a, int b)
{
 int sum = a + b;
 return sum;
}
// adding three integer values.
public int Add(int a, int b, int c)
{
 int sum = a + b + c;
 return sum;
}
// Main Method
public static void Main(String[] args)
{
 // Creating Object
 DNT ob = new DNT();
 int sum1 = ob.Add(1, 2);
 Console.WriteLine("sum of the two "
 + "integer value : " + sum1);
 int sum2 = ob.Add(1, 2, 3);
 Console.WriteLine("sum of the three "
 + "integer value : " + sum2);
}
}

Explanation

This C# program in the C# Compiler demonstrates function overloading by defining two Add methods in the DNT class. One method takes two integer parameters and adds them, while the other takes three integer parameters and calculates their sum. The appropriate method is called based on the number of parameters provided, showcasing the concept of function overloading.

Output

sum of the two integer value : 3
sum of the three integer value : 6

Example 2: The data types of the parameters of methods.

 // C# program to demonstrate the function overloading by changing the Data types of the parameters 
using System;
class DNT {
// adding three integer values.
public int Add(int a, int b, int c)
{
 int sum = a + b + c;
 return sum;
}
// adding three double values.
public double Add(double a,
 double b, double c)
{
 double sum = a + b + c;
 return sum;
}
// Main Method
public static void Main(String[] args)
{
// Creating Object
 DNT ob = new DNT();
int sum2 = ob.Add(1, 2, 3);
Console.WriteLine("sum of the three "
+ "integer value : " + sum2);
double sum3 = ob.Add(1.0, 2.0, 3.0);
Console.WriteLine("sum of the three "
+ "double value : " + sum3);
}
}

Explanation

This C# program demonstrates function overloading by defining two Add methods in the DNT class. One method adds three integers, while the other adds three doubles. The appropriate method is called based on the data types of the provided arguments. The program creates an instance of the DNT class, calls both methods and prints the results for both integer and double additions.

Output

sum of the three integer value : 6
sum of the three double value : 6 

Example 3: The Order of the parameters of methods.

 // C# program to demonstrate the function overloading by changing the Order of the parameters
using System;
class DNT {
// Method
public void Identity(String name, int id)
{
Console.WriteLine("Name1 : " + name + ", "
 + "Id1 : " + id);
}
 // Method
public void Identity(int id, String name)
{
Console.WriteLine("Name2 : " + name + ", "
 + "Id2 : " + id);
}
// Main Method
public static void Main(String[] args)
{
// Creating Object
DNT obj = new DNT();
obj.Identity("Urmi", 1);
obj.Identity(2, "Rumi");
}
}

Explanation

This C# program demonstrates function overloading by defining two Identity methods in the DNT class. One method takes a string followed by an integer as parameters, while the other takes an integer followed by a string. The appropriate method is called based on the order of the provided arguments, showcasing function overloading based on parameter order. When the program is run, it prints the names and IDs in different orders using the overloaded methods.

Output

Name1 : Urmi, Id1 : 1
Name2 : Rumi, Id2 : 2

Method Overriding in C#

In C#, method overriding allows a subclass to provide a specific implementation for a method that is already defined in its superclass. The overridden method in the subclass should have the same name, return type, and parameters as the method in the superclass, enabling polymorphic behavior and facilitating customization in derived classes.

Why do we need Method Overriding in C#?

  • Method overriding allows different classes to be used in the same way through their common base class.
  • It lets subclasses change inherited methods to fit their own needs.
  • Base class methods can be reused in many subclasses, saving time and effort.
  • The right method is chosen when the program runs, based on the object's real type.
  • It makes the code easier to change and grow over time.

Important points to remember

  • The overriding method must have the same name, return type, and parameters as the base method.
  • The overriding method’s access level can be the same or more open than the base method, but not more restrictive.
  • The base method must be marked virtual, and the child method must use override.
  • Overriding allows treating derived class objects as base class objects (polymorphism).
  • The correct method is chosen at runtime based on the object’s actual type.
  • The base keyword lets the child class call the parent’s version of the method.
  • A method can be marked sealed to stop further overriding in subclasses.

Types of Keywords for Method Overriding in C#

There are three types of keywords for method overriding in C#

  • Virtual Keywords
  • Override Keyword
  • Base Keyword

Virtual Keyword

In C#, the virtual keyword is used to declare a method, property, or event in a base class that can be overridden by derived classes. When a method is declared as virtual, it means that the method can have an implementation in the base class, but it can also be overridden (redefined) in any derived class.

Example

 using System;

class Animal
{
 // Virtual method
 public virtual void MakeSound()
 {
 Console.WriteLine("The animal makes a generic sound.");
 }
}
class Dog : Animal
{
 // Override the virtual method
 public override void MakeSound()
 {
 Console.WriteLine("The dog barks.");
 }
}
class Cat : Animal
{
 // Override the virtual method
 public override void MakeSound()
 {
 Console.WriteLine("The cat meows.");
 }
}
class Program
{
 static void Main()
 {
 Animal myAnimal = new Animal();
 Animal myDog = new Dog();
 Animal myCat = new Cat();
 myAnimal.MakeSound();
 myDog.MakeSound(); 
 myCat.MakeSound(); 
 }
}

Explanation

In this example, the MakeSound method is declared as virtual in the Animal base class. Both the Dog and Cat classes inherit from Animal and override the MakeSound method with their own implementations. When calling MakeSound on instances of Animal, Dog, or Cat, the appropriate overridden method is executed, demonstrating polymorphism.

Output

The animal makes a generic sound.
The dog barks.
The cat meows.

Override Keyword

In C#, the override keyword is used to indicate that a method, property, indexer, or event in a derived class is intended to override a method, property, indexer, or event in a base class. It provides a way to provide a specific implementation for a virtual or abstract member declared in a base class. When you use the override keyword, you are telling the compiler that you are intentionally overriding a member from the base class.

Example

 using System;
class Shape
{
 public virtual void Draw()
 {
 Console.WriteLine("Drawing a shape");
 }
}
class Circle : Shape
{
 public override void Draw()
 {
 Console.WriteLine("Drawing a circle");
 }
}
class Square : Shape
{
 public override void Draw()
 {
 Console.WriteLine("Drawing a square");
 }
}
class Program
{
 static void Main()
 {
 Shape shape1 = new Circle();
 Shape shape2 = new Square();

 shape1.Draw(); // Output: Drawing a circle
 shape2.Draw(); // Output: Drawing a square
 }
}

Explanation

In this example in the C# Editor, the Shape class has a virtual method called Draw(). The Circle and Square classes inherit from Shape and use the override keyword to provide specific implementations of the Draw() method. When creating objects of Circle and Square classes and calling the Draw() method through the base class reference, the overridden methods in the respective derived classes are invoked, demonstrating polymorphic behaviour in C#.

Output

Drawing a circle
Drawing a square

Base Keyword

This is how derived classes can access members of the base class. Basically, it was used to access the base class' constructors, methods, and functions. A static method cannot utilize the basic keyword. The base keyword indicates which base class's constructor should be used for creating instances of the derived class.

Example

 // C# program to show the use of 'base' keyword in method overriding
using System;

// base class
public class web {
string name = "DotNetTricks";

// 'showdata()' is member method,
// declare as virtual
public virtual void showdata()
{
 Console.WriteLine("Website Name: " + name);
}
}
// derived class
// class 'web' is inherits
// class 'stream'
class stream : web {
string s = "Computer Science";
 
//'showdata()' is overridden
// in derived class
public override void showdata()
{
 
 // Calling 'showdata()' of base
 // class using 'base' keyword
 base.showdata();
 Console.WriteLine("About: " + s);
}
}
class DNT {
 
// Main Method
static void Main()
{
 // 'E' is object of class stream
 // also works as object of 
 // class 'web'
 stream E = new stream();
 // it first invokes 'showdata()'
 // of class 'web' then it invokes 
 // 'showdata()' of class 'stream'
 E.showdata(); 
}
}

Explanation

This C# program demonstrates method overriding using the base keyword. A base class web has a method showdata(), and a derived class stream overrides it. The base.showdata() call inside the derived class invokes the base class method before extending functionality, displaying both base and derived class outputs.

Output

Website Name: DotNetTricks 
About: Computer Science
Conclusion

In conclusion, method overloading and method overriding in C#are powerful techniques that enhance code flexibility and reusability. Method overloading enables defining multiple methods with the same name but different parameters, enhancing versatility. Method overriding allows customizing inherited methods, promoting polymorphism and dynamic behavior. Mastering these concepts is essential for effective object-oriented programming in C#.

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Live Training - Book Free Demo
Advanced Full-Stack .NET Developer with Gen AI Certification Training
15 Jun
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
15 Jun
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Azure Developer Certification Training
16 Jun
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
21 Jun
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Full-Stack Azure AI Engineer Certification Training Program
21 Jun
05:30PM - 07:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this