Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C#
  4. Understanding Polymorphism in C#: A Comprehensive Guide

Understanding Polymorphism in C#: A Comprehensive Guide

Date- Mar 15,2026 77
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
Mastering TypeScript Classes and Object-Oriented Programming for Scalable Applications
Mar 26, 2026
Understanding Interfaces and Abstract Classes in Java: A Comprehensive Guide
Mar 16, 2026
Previous in C#
Understanding Inheritance in C#: A Comprehensive Guide
Next in C#
Understanding Encapsulation and Access Modifiers in C#
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,938 views
  • 2
    Error-An error occurred while processing your request in .… 11,273 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 235 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,459 views
  • 5
    Mastering JavaScript Error Handling with Try, Catch, and F… 162 views
  • 6
    Mastering Unconditional Statements in C: A Complete Guide … 21,497 views
  • 7
    Unable to connect to any of the specified MySQL hosts 6,232 views

On this page

🎯

Interview Prep

Ace your C# interview with curated Q&As for all levels.

View C# Interview Q&As

More in C#

  • Zoom C# Wrapper Integration 12905 views
  • Convert HTML String To Image In C# 11510 views
  • The report definition is not valid or is not supported by th… 10880 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9871 views
  • Get IP address using c# 8700 views
View all C# posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
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 | 1770
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
Code2Night

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

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor