---
title: "Polymorphism in C#.Net"  
description: "Polymorphism gives ability how my object works in different situation.Polymorphism means same operation may behave differently on different classes.Po"  
author: "AVADHESH PATEL"  
published: 2012-07-09  
updated: 2017-11-30  
canonical: https://www.mindstick.com/articles/959/polymorphism-in-c-sharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Polymorphism in C#.Net

Polymorphism gives ability how my object works in different situation.Polymorphism means same operation may behave differently on different classes.Polymorphism means having more than one form. It allows you to invoke [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) methods through a base class reference during run-time.\

Overloading and overriding are used to implement polymorphism.

Polymorphism is classified into compile time polymorphism or early binding or static binding and Runtime polymorphism or [late binding](https://www.mindstick.com/forum/33930/difference-between-early-binding-and-late-binding-in-c-sharp) or [dynamic binding](https://www.mindstick.com/forum/48/what-is-dynamic-binding-in-c-sharp).

##### There are 6 way to achieve polymorphism:

1. Operator overloading

2. [Method overloading](https://www.mindstick.com/articles/12033/method-overloading-in-c-sharp)

3. Virtual function

4. Generic

5. Interface

6. [Abstract class](https://www.mindstick.com/articles/1430/abstract-class)

##### Method overloading or Compile Time Polymorphism:

In the example there is one function with two different arguments, one have integer and one have string. So when a function perform different – different task with different – different argument that called [function overloading](https://www.mindstick.com/forum/159468/how-does-c-plus-plus-handle-function-overloading-and-why-is-it-useful).

##### Method Overriding or Run Time Polymorphism:

```
using System;using System.Collections.Generic;using System.Linq; namespace abstarction{    public abstract class university    {        public abstract void BTech();        public abstract void MBA();    }     public class GBTU : university    {        public override void BTech()        {            Console.WriteLine("GBTU BTech Fee 50000/-");     
  }         public override void MBA()        {            Console.WriteLine("GBTU MBA Fee 100000/-");        }    }     public class MTU : university    {        public override void BTech()        {            Console.WriteLine("MTU BTech Fee 40000/-");        }         public override void MBA()        {            Console.WriteLine("MTU MBA Fee 800000/-");        }    }      class Program    {        static void Main(string[] args)        {            GBTU g = new GBTU();            g.BTech();            g.MBA();            MTU m = new MTU();            m.BTech();            m.MBA();            Console.ReadLine();        }    }} 
```

##### Output-

GBTU BTech Fee 50000/-

GBTU MBA Fee 100000/-

MTU BTech Fee 40000/-

MTU MBA Fee 800000/-

##### Polymorphism via Generic:

First argument

```
class test<temp>    {        public temp show(temp t)        {            return t;        }    }     class Program    {        static void Main(string[] args)        {            test<int> i = new test<int>();            Console.WriteLine(i.show(22));            test<string> s = new test<string>();            Console.WriteLine(s.show("hello..."));            Console.ReadLine();        }    }
```

Output:

22

Hello…

Second Argument

```
class test<temp>    {        public temp show(temp t)        {            return t;        }    }     class Program    {        static void Main(string[] args)        {            test<int> i = new test<int>();            Console.WriteLine(i.show(10));            test<string> s = new test<string>();            Console.WriteLine(s.show("How are
you..."));            Console.ReadLine();        }    }
```

Output:\

10

How are you…

Note. Through generic object we can call [generic and non generic](https://www.mindstick.com/interview/33647/what-is-the-generic-and-non-generic-collections) function in [generic class](https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example).

---

Original Source: https://www.mindstick.com/articles/959/polymorphism-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
