Method Overloading and Overriding in c#
In the C# programming language, method overloading and method overriding are two important features or concepts related to base class or subclass methods. While they generally appear similar, their purpose or use in C# programming differs for multiple individual programming tasks, and method overloading and method overriding are used in different contexts in C# programming.

So, let’s explore the concept of method overloading and method overriding in C# programming.
Method Overloading Concept in C#
Method overloading in C# programming occurs when a C# programmer defines or creates multiple class methods with the same name within a single user-defined class. However, the class variable parameters’ data type, number of parameters, or both are individually defined separately. These data types can have the same or different return data types. But this does not differentiate between the overloaded method itself.
In a class program, method overloading is executed at compile-time. It allows for multiple method steps to call a user-defined class method.
Key features of method overloading in C# programming.
- Remember, the name of the created method must be the same as the name of the method defined in the existing class.
- User-defined class methods must have different variable parameter list data types, either in the number of class parameters or in their data types defined.
- While the return data type may be different in the class you create, it is not enough to distinguish between class overloaded methods in a program based solely on the return data type.
- Method overloading in a class program is automatically corrected at compile-time by static polymorphism.
Example of method overloading in C#.
using System;
public class addition
{
// here we define Overloaded class method 1: which Adds two p and q integers value
public int Total(int p, int q)
{
return p + q;
}
// here we define Overloaded class method 2: which Adds three p, q, and r, integers values
public int Total(int p, int q, int r)
{
return p + q + r;
}
// here we define Overloaded class method 3: which Adds two double numbers values
public double Total(double p, double q)
{
return p + q;
}
}
public class program
{
static void Main()
{
Addition addition = new Addition();
// here we are calling different overloaded class methods
Console.WriteLine(addition.Total(7, 7)); // Result is – 14
Console.WriteLine(addition.Total(4, 7, 1)); // Result is – 12
Console.WriteLine(addition.Total(8.3, 4.9)); // Result is – 13.2
}
}
Explaining method overloading in C#.
- In this program, the Total method is overloaded with different class parameter signature values.
- Total(int, int)
- Total(int, int, int)
- Total(double, double)
- When the system calls the Total() class method, the C# program compiler selects the proper class method based on the arguments passed to it.
Method overriding concept in C#.
Method overriding in the C# programming language is an important feature of the object-oriented programming (OOPS) concept. Method overriding involves defining a method in a derived class to provide a special implementation of a class method already declared in the base class. The method in the base or superclass must be marked or indicated with the virtual keyword (or the abstract class keyword) so that it can be overridden in a derived or subclass in the current program if needed.
Overriding methods created or defined in a class are automatically corrected at program runtime. This is why dynamic polymorphism is a feature of modern programming.
Method overriding concept in C#.
To override a class method in a user-defined base or superclass, a C# programmer must mark or indicate the method as virtual, abstract, or protected in the existing class program.
The signature of a class method defined in a derived or subclass (class method name, return data type, and class variable parameters) must match the exact proper method of the base class.
A class method overridden in a derived or subclass provides a new implementation of the super or base class method.
In C# programs, class overriding of a method is corrected at program runtime; this is known as dynamic polymorphism.
Example of class method overriding in C#.
Using System;
public class courses
{
// here we use Virtual class method in the base/super class
public virtual void Select()
{
Console.WriteLine(“Select your desire programming course.”);
}
}
public class Python : Course
{
// here we define Override the Select method from the Course class
public override void Select()
{
Console.WriteLine(“You select the python programming.”);
}
}
public class Sql : Course
{
// here we Override the Select method from the Course class
public override void Select()
{
Console.WriteLine(“You select the sql database.”);
}
}
public class program
{
static void Main()
{
// here we Create an Course reference but point it to Python and Sql class objects
Course python = new Python();
Course sql = new Sql();
// Here we call the overridden Select method
python.Select(); // Result is – You select the Python programming.
sql.Select(); // Result is – You select the SQL database.
}
}
Explaining class method overriding.
- In this program, the Select method is defined as virtual in the Course base class.
- Whereas the Python and SQL classes override this method to provide specific behavior for each type of Course class.
- When calling Select() on python (which indicates Python) and sql (which indicates SQL), the runtime system decides which class method to call based on the actual class object type (Python or SQL), whether the references are of the Course type. This is the function of runtime polymorphism.
When to use overloading vs. overriding in C#.
When to use method overloading in C#.
When a C# programmer needs to provide multiple versions of a class method, each individually separated by the variable parameter data type or number declared in the existing class program.
- Example – For example, a class program has a method for summing multiple numbers, which simultaneously accepts arguments of different class data types or numbers as input.
When to use C# method overriding.
When a C# programmer needs to allow a derived or subclass to modify or further extend the behavior of a class method defined in a base parent class.
- Example – A parent class is defined with a Draw method as a base class, and derived subclasses Circle and Rectangle provide their own implementations of Shape and Draw, respectively.
Group examples of overloading and overriding concepts in C#.
So, let’s understand the concept of method overloading and method overriding in C# programming with a simple example.
using System;
public class Course
{
public virtual void Select()
{
Console.WriteLine(“You can select your desired programming course.”);
}
// Here we use method overloading in the base class
public void Select(string selection)
{
Console.WriteLine($”Your course selection is {selection} selection.”);
}
}
public class Java : Course
{
public override void Select() // Here we override it
{
Console.WriteLine(“Your selection is Java language.”);
}
// Here we use method overloading in the derived class
public void Select(int value)
{
for (int p = 0; p < value; p++)
{
Console.WriteLine(“\n | Vcanhelpsu For Programming |”);
}
}
}
public class Program
{
static void Main()
{
Course course = new Course();
course.Select(); // here it calls the base class Select()
Java java = new Java();
java.Select(); // here it calls the overridden Select() in the Java class
java.Select(“,Best”); // here it calls the overloaded Select() in the Course class
java.Select(7); // here it calls the overloaded Select() in the Java class
}
}
Explain overloading and overriding concepts.
- The program example here groups method overloading and method overriding. The Course class defines an overloaded Select method that takes a string as a variable parameter.
- The program overrides the Select method to provide a specialized implementation in the Java class.
- The Java class also overloads the Select method with an integer parameter, allowing you to print the “Best” selection text statement multiple times to the console screen.
Main Differences Between Method Overloading and Method Overriding in c#.
| Feature | Method Overloading concept in c# | Method Overriding concept in c# |
| About/Definition | Method overloading features help us to declare or defining multiple class methods at a same time with the same name but different list of variable or class parameter lists. | Method overriding features help us to Redefining a base parent class method in the derived subclass with the same signature identity. |
| Parameters list | In Method overloading concept Must have different class parameter lists argument (number or variable and their data types). | In Method overriding features Must have the same class signature (with name, return data type, list of variable or parameters, etc). |
| Return data Type | With Method overloading concept program Return type can different, but here data type return type alone cannot be differentiating the overloaded class methods. | In Method overriding concept Return type must be the same as the base class method. |
| Binding method | With Method overloading Compile-time binding (static polymorphism) supported. | With Method overriding Runtime binding (dynamic polymorphism) feature supported. |
| Base Class Method | In Method overloading there is Not required for method overloading. | In The Method overriding base class method must be virtual, abstract, or protected to allow overriding. |
| What is Purpose | Method overloading Allows us to multiple versions of the same class method with name with different argument lists. | Method overriding Allows us a subclass to provide a specific implementation of a method that is already defined in the base or parent class. |
| Grant to Base Class | Method overloading doesn’t involve a base class method concept. | Method overriding Can call the base class method using base.MethodName() function. |
Method overloading and method overriding summary in C#.
- The method overloading concept in C# programming allows C# users to declare or define multiple class methods with the same name but different parameter lists, providing different steps or methods to call an existing class method. Class method overriding in a C# program allows a derived subclass to provide a specialized implementation of a class method already declared or defined in the base class, supporting runtime polymorphism features in the existing program.
- Both method overloading and method overriding are essential features or concepts in object-oriented programming to implement or implement class programs for flexibility, reliability, and reusability of program source code. In which class method program overriding focuses on compile-time behavior, and class method overriding focuses totally on runtime behavior.
