---
title: "Overloading and Overriding in c#"  
description: "In this article, I am going to discuss Method Overloading in C# with Examples."  
author: "Rashmi Sharma"  
published: 2021-05-13  
updated: 2021-05-14  
canonical: https://www.mindstick.com/articles/326524/overloading-and-overriding-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Overloading and Overriding in c#

**[Method Overloading](https://www.mindstick.com/forum/156845/what-is-method-overloading-in-c-sharp-give-an-example) :-**

1- It is a process of creating a method with the same name and different functionality. In simple words, a method will have same name but its functionality will be different.

2- we can also achieve method overloading in Inheritance. it allows us to create a method in the super class with same name having different behaviour in super class and sub class.

3- A overloaded method will have different parameters.

**When we overload [methods in C#](https://answers.mindstick.com/qa/82407/what-is-methods-in-c-sharp)?**

If you want to perform the same operation but with different types of argument, different types of values.

**For example, if you want to add two integers, two floats, and two strings.**

class Calculate

{

public void add(int a, int b)

{

Console.WriteLine('Sum of int:- '+a + b);

}

public void add(float a, float b)

{

Console.WriteLine('Sum of float vlaues :- '+a + b);

}

public void add(string FirstName, string LastName)

{

Console.WriteLine('Name :- '+ FirstName +' '+ LastName);

}

static void Main(string[] args)

{

Calculate obj = new Calculate();

obj.add(5, 7);

obj.add(7.5f, 8.5f);

obj.add('Rashmi', 'Sharma');

}

}

**output:-**

Sum of int values :- 57

Sum of float values:- 7.58

Name :- Rashmi Sharma

**[Method Overriding](https://www.mindstick.com/forum/53/method-overriding) :-**

1- It is a process of creating a method with the same [name and same signature](https://www.mindstick.com/forum/228/can-we-create-two-constructor-of-same-name-and-same-signature-in-same-class) in different classes not in same class. In simple words, a method will have same name and parameters will be in different class(super class and sub class).

2- The superclass method is called the overridden method and the sub-class method is called the overriding method.

2- we can achieve method overriding in Inheritance only. it us to create a method in the super class with same name and parameters reimplementing functionality of super class in sub class.

**When we override methods in C#?**

If the superclass required to be add some implementation then the subclass override that method with the required logic.

**How to implement overriding method**

syntax :-

Class1

{

Public virtual void show(){} //[virtual function](https://www.mindstick.com/forum/161768/what-is-virtual-function-and-why-is-it-used-with-example) (overridable)

}

Class2: Class1

{

Public override void show(){} //overriding

}

**For example, if you want to add two integers.**

class Class1

{

public virtual void add( int a, int b)

{

Console.WriteLine('result of two numbers :- ' + (a+b));

}

}

class Class2 : Class1

{

public override void add(int a, int b)

{

Console.WriteLine('result of two numbers :- '+ ( a +b)/2);

}

}

class Program

{

static void Main(string[] args)

{

Class2 obj2 = new Class2();\

obj2.add(7,9);

}

}

**Output:-**

result of two numbers :- 5

**[Difference between Method](https://www.mindstick.com/interview/2444/difference-between-method-and-selector) overloading and Method [overriding in C#](https://answers.mindstick.com/qa/82455/what-is-the-difference-between-overloding-and-overriding-in-c-sharp)**

Method Overloading :-

1- In this concept multiple methods can be define with the same name but with a different parameters.

2- Overloading a method can be define within a class or within the child classes also.

3- No keywords are used to implement [function overloading](https://www.mindstick.com/forum/159468/how-does-c-plus-plus-handle-function-overloading-and-why-is-it-useful).

Method Overriding :-

1- In this concept multiple methods can be define with the same name and same parameters also changes functionality only.

2- Overriding of methods is not possible within the same class it must be define under the child classes.

3- Use the [virtual keyword](https://www.mindstick.com/forum/159480/explain-the-purpose-of-the-virtual-keyword-and-virtual-functions-in-c-plus-plus) for the base class function and override keyword in the [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) function to implement function overriding.\
\

---

Original Source: https://www.mindstick.com/articles/326524/overloading-and-overriding-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
