Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

Understanding Polymorphism in C#: A Comprehensive Guide

Date- Mar 15,2026

10

c# polymorphism

Overview of Polymorphism

Polymorphism is a core principle of object-oriented programming that allows methods to do different things based on the object that it is acting upon. In C#, polymorphism enables methods to be used interchangeably, increasing the flexibility and maintainability of your code. Understanding this concept is crucial for designing scalable systems and writing clean code.

Prerequisites

  • Basic knowledge of C# syntax
  • Understanding of classes and objects
  • Familiarity with inheritance and interfaces
  • Basic understanding of object-oriented programming concepts

Types of Polymorphism

Compile-Time Polymorphism

Compile-time polymorphism, also known as static polymorphism, is achieved through method overloading and operator overloading. The decision about which method to call is made at compile time.

using System;

class MathOperations {
    public int Add(int a, int b) {
        return a + b;
    }
    public double Add(double a, double b) {
        return a + b;
    }
}

class Program {
    static void Main() {
        MathOperations math = new MathOperations();
        Console.WriteLine(math.Add(5, 10));     // Calls the first Add method
        Console.WriteLine(math.Add(5.5, 10.5)); // Calls the second Add method
    }
}

In this code:

  • The MathOperations class contains two Add methods: one for integers and another for doubles.
  • In the Main method, we create an instance of MathOperations.
  • When invoking Add with integers, the first method is called, while with doubles, the second is called.

Run-Time Polymorphism

Run-time polymorphism, or dynamic polymorphism, is achieved through method overriding. It allows a method to be invoked based on the object's runtime type, rather than its compile-time type.

using System;

class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal speaks");
    }
}

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("Dog barks");
    }
}

class Cat : Animal {
    public override void Speak() {
        Console.WriteLine("Cat meows");
    }
}

class Program {
    static void Main() {
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        myDog.Speak(); // Outputs: Dog barks
        myCat.Speak(); // Outputs: Cat meows
    }
}

In this example:

  • The Animal class has a virtual method Speak.
  • The Dog and Cat classes override this method.
  • In Main, we create instances of Dog and Cat as Animal references.
  • When Speak is called, the overridden methods are executed based on the actual object type.

Polymorphism with Interfaces

Polymorphism can also be achieved using interfaces. An interface defines a contract that implementing classes must fulfill, allowing for interchangeable objects.

using System;

interface IShape {
    double Area();
}

class Circle : IShape {
    private double radius;
    public Circle(double r) {
        radius = r;
    }
    public double Area() {
        return Math.PI * radius * radius;
    }
}

class Square : IShape {
    private double side;
    public Square(double s) {
        side = s;
    }
    public double Area() {
        return side * side;
    }
}

class Program {
    static void Main() {
        IShape circle = new Circle(5);
        IShape square = new Square(4);
        Console.WriteLine(circle.Area()); // Outputs the area of the circle
        Console.WriteLine(square.Area()); // Outputs the area of the square
    }
}

This code demonstrates:

  • The IShape interface defines a method Area.
  • The Circle and Square classes implement this interface and provide their own versions of Area.
  • In the Main method, we create instances of Circle and Square as IShape references.
  • Calling Area outputs the respective areas based on the actual object type.

Polymorphism in Real-World Applications

Polymorphism is widely used in frameworks and libraries, allowing for more maintainable and extensible code. For instance, in GUI applications, different button types can be treated as a single button type.

using System;

abstract class Button {
    public abstract void Click();
}

class WindowsButton : Button {
    public override void Click() {
        Console.WriteLine("Windows Button Clicked");
    }
}

class MacButton : Button {
    public override void Click() {
        Console.WriteLine("Mac Button Clicked");
    }
}

class Program {
    static void Main() {
        Button btn1 = new WindowsButton();
        Button btn2 = new MacButton();
        btn1.Click(); // Outputs: Windows Button Clicked
        btn2.Click(); // Outputs: Mac Button Clicked
    }
}

In this example:

  • The Button class is abstract and defines an abstract method Click.
  • The WindowsButton and MacButton classes override the Click method.
  • In Main, we create instances of the buttons as Button references.
  • The Click method is executed based on the actual object type, demonstrating polymorphism in action.

Best Practices and Common Mistakes

When working with polymorphism, consider the following best practices:

  • Use interfaces for better abstraction and flexibility.
  • Avoid excessive use of polymorphism as it can lead to complexity.
  • Ensure that overridden methods maintain the same behavior expectations (Liskov Substitution Principle).

Common mistakes include:

  • Not using virtual/override keywords properly.
  • Failing to implement all interface methods in derived classes.
  • Overcomplicating designs by overusing polymorphism.

Conclusion

Polymorphism is a powerful feature in C# that enhances the flexibility and maintainability of your code. By understanding both compile-time and run-time polymorphism, as well as implementing interfaces, you can design systems that are easier to extend and maintain. Remember to apply best practices to avoid common pitfalls, ensuring your code remains clean and efficient.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Abstract Classes in C#: A Comprehensive Guide
Mar 16, 2026
Understanding Interfaces in C#: A Comprehensive Guide
Mar 15, 2026
Understanding Inheritance in C#: A Comprehensive Guide
Mar 15, 2026
Mastering Object-Oriented Programming in C#: A Comprehensive Guide
Mar 15, 2026

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers