---
title: "What is the overriding and overloading in C# with example?"  
description: "What is the overriding and overloading in C# with example?"  
author: "Abhishek Srivasatava"  
published: 2016-09-17  
updated: 2020-09-17  
canonical: https://www.mindstick.com/interview/23045/what-is-the-overriding-and-overloading-in-c-sharp-with-example  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# What is the overriding and overloading in C# with example?

**Overloading:** This concept is useful when we need the function / method of the same name but different signature or parameter or type. It is the example of compile time polymorphism. \

**Example**

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication15{    public classprogram1    {        public int add(int a, int b)         {                     return a + b;         }        public int add(int a, int b, int c)         {                     return a + b + c;         }        }    }       class Program    {        static void Main(string[] args)        {     
      program1 p1 = new program1();      program1 p1 = new program1();      int b = p1.add(2, 3);      int c = p1.add(2, 3,4);      Console.WriteLine("{0}{1}",b,c);          
      Console.ReadLine();      Console.ReadLine();        }    }}
```

**Overriding:** Concept for the overriding is useful when we need a method / function of the same name, parameter, type and the signature which is defined in base and derived class. The advantage of this is that memory will allocate only for one method/function. It is the example of Dynamic time polymorphism.

It can be done by using the keyword Virtual (before the function name define in base class) and override (before the function name define in derived class)

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication15{    public class program1    {            public virtual int fun()      {          return 55; 
    }    }  public class program2 :program1{           public override int fun()      {          return 90; 
    }     }      class Program    {        static void Main(string[] args)        {                                    program1 p1 = new program1();            program2 p2 = new program2();             int  b= p1.fun();                        int c = p2.fun();            Console.WriteLine("{0}\n{1}",b,c);            Console.ReadLine();         }    }}  
```

## Answers

### Answer by Abhishek Srivasatava

**Overloading:** This concept is useful when we need the function / method of the same name but different signature or parameter or type. It is the example of compile time polymorphism. \

**Example**

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication15{    public classprogram1    {        public int add(int a, int b)         {                     return a + b;         }        public int add(int a, int b, int c)         {                     return a + b + c;         }        }    }       class Program    {        static void Main(string[] args)        {     
      program1 p1 = new program1();      program1 p1 = new program1();      int b = p1.add(2, 3);      int c = p1.add(2, 3,4);      Console.WriteLine("{0}{1}",b,c);          
      Console.ReadLine();      Console.ReadLine();        }    }}
```

**Overriding:** Concept for the overriding is useful when we need a method / function of the same name, parameter, type and the signature which is defined in base and derived class. The advantage of this is that memory will allocate only for one method/function. It is the example of Dynamic time polymorphism.

It can be done by using the keyword Virtual (before the function name define in base class) and override (before the function name define in derived class)

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication15{    public class program1    {            public virtual int fun()      {          return 55; 
    }    }  public class program2 :program1{           public override int fun()      {          return 90; 
    }     }      class Program    {        static void Main(string[] args)        {                                    program1 p1 = new program1();            program2 p2 = new program2();             int  b= p1.fun();                        int c = p2.fun();            Console.WriteLine("{0}\n{1}",b,c);            Console.ReadLine();         }    }}  
```


---

Original Source: https://www.mindstick.com/interview/23045/what-is-the-overriding-and-overloading-in-c-sharp-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
