Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • C# - Get Started
  • C# - Version History
  • C# - First Program
  • C# - Keywords
  • C# - Class and Objects
  • C# - Namespace
  • C# - Variables
  • C# - Implicitly-Typed Variables
  • C# - Data Types
  • Numbers
  • Strings
  • DateTime
  • Structure
  • Enum
  • StringBuilder
  • Anonymous Types
  • Dynamic Types
  • Nullable Types
  • C# - Value & Reference Types
  • C# - Interface
  • C# - Operators
  • C# - if else Statements
  • C# - Ternary Operator ?:
  • C# - Switch
  • C# - For Loop
  • C# - While Loop
  • C# - Do-while Loop
  • C# - Partial Class
  • C# - Static
  • C# - Array
  • Multidimensional Array
  • Jagged Array
  • C# - Indexer
  • C# - Generics
  • Generic Constraints
  • C# - Collections
  • ArrayList
  • List
  • SortedList
  • Dictionary
  • Hashtable
  • Stack
  • Queue
  • C# - Tuple
  • C# - ValueTuple
  • C# - Built-in Exceptions
  • Exception Handling
  • throw
  • Custom Exception
  • C# - Delegates
  • Func Delegate
  • Action Delegate
  • Predicate Delegate
  • Anonymous Methods
  • C# - Events
  • C# - Covariance
  • C# - Extension Method
  • C# - Stream I/O
  • C# - File
  • C# - FileInfo
  • C# - Object Initializer
  • OOP - Overview
  • Object-Oriented Programming
  • Abstraction
  • Encapsulation
  • Association & Composition
  • Inheritance
  • Polymorphism
  • Method Overriding
  • Method Hiding
  • C# - Solid Principles
  • Single Responsibility Principle
  • Open/Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle
  • Design Patterns
  • Singleton
  • Abstract Factory
  • Factory Method
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Polymorphism

Polymorphism is a Greek word that means multiple forms or shapes. You can use polymorphism if you want to have multiple forms of one or more methods of a class with the same name.

In C#, polymorphism can be achieved in two ways:

  1. Compile-time Polymorphism
  2. Run-time Polymorphism

Compile-time Polymorphism (Method Overloading)

Compile-time polymorphism is also known as method overloading. C# allows us to define more than one method with the same name but with different signatures. This is called method overloading.

Method overloading is also known as early binding or static binding because which method to call is decided at compile time, early than the runtime.

Rules for Method Overloading:

  1. Method names should be the same but method signatures must be different. Either the number of parameters, type of parameters, or order of parameters must be different.
  2. The return type of the methods does not play any role in the method overloading.
  3. Optional Parameters take precedence over implicit type conversion when deciding which method definition to bind.

The following example demonstrates the method overloading by defining multiple Print() methods with a different number of parameters of the same type.

Example: Method Overloading Method Overloading
class ConsolePrinter
{
    public void Print(string str){ 
        Console.WriteLine(str);
    }

    public void Print(string str1, string str2){ 
        Console.WriteLine($"{str1}, {str2}");
    }

    public void Print(string str1, string str2, string str3){ 
        Console.WriteLine($"{str1}, {str2}, {str3}");
    }
}

The following example demonstrates polymorphism using the same number of parameters with different types.

Example: Method Overloading
class ConsolePrinter
{
    public void Print(string str){ 
        Console.WriteLine(str);
    }

    public void Print(int a){ 
        Console.WriteLine($"Integer {a}");
    }
}

The following example demonstrates polymorphism using the same number of parameters with different sequences.

Example: Method Overloading
class ConsolePrinter
{
    public void Print(int a, string str){ 
        Console.WriteLine($"{a}, {str}");
    }

    public void Print(string str, int a){ 
        Console.WriteLine($"{a}, {str}");
    }
}

Please note that return type of methods is not considered. The following will give a compile-time error.

Example: Invalid Method Overloading
class ConsolePrinter
{
    public void Print(int a, string str)
    { 
        Console.WriteLine($"{a}, {str}");
    }

    public int Print(int a, string str){ 
        Console.WriteLine($"{a}, {str}");
        return 0;
    } 
}

The following methods overloaded with different types, sequence, and number of parameters.

Example: Method Overloading
class ConsolePrinter
{
     public void Print(string str){ 
        Console.WriteLine(str);
    }

    public void Print(string str1, string str2){ 
        Console.WriteLine($"{str1}, {str2}");
    }

    public void Print(string str1, string str2, string str3){ 
        Console.WriteLine($"{str1}, {str2}, {str3}");
    }
	
    public void Print(int a){ 
        Console.WriteLine($"Integer {a}");
    }
	
	 public void Print(int a, string str){ 
        Console.WriteLine($"{a}, {str}");
    }

    public void Print(string str, int a){ 
        Console.WriteLine($"{a}, {str}");
    }
}

Invoking Overloaded Methods

We can call the overloaded method by passing the exact parameter it requires. For example, if we want to invoke the print(string str) method that displays a string value, we will pass only one argument of string type. Likewise, if we want to invoke the Print(int a, string str) method, we will pass int and string type argument.

Example: Method Overloading
public static void Main()
{
	ConsolePrinter cp = new ConsolePrinter();
	cp.Print("Hello World!");
	cp.Print(1, "John");
}
Try it

Thus, polymorphism using method overloading plays important role in designing an application. The simplest example in .NET API is the overloads of Console.WriteLine() methods.

Learn about runtime polymorphism next.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.