Tech Point Fundamentals

Sunday, November 14, 2021

Static Polymorphism and Overloadings in C#

 Static Polymorphism and Overloadings in C#

Method-Overloading-CSharp

Polymorphism is one of the main pillar concepts of OOPS after Abstraction, Encapsulation, and Inheritance.  Polymorphism provides the ability for the developer to implement different methods with the same name. 

Polymorphism can be Static or Dynamic. In one of our previous articles, we have learned about Dynamic PolymorphismPlease read the Dynamic Polymorphism article here. In this article, we will walk through Static Polymorphism and Overloadings





Static Polymorphism (Early Binding)


  1. Static Polymorphism is also known as Compile-Time Polymorphism,  Compile-Time Binding, Early Binding, or Static Binding.  
  2. In Static Polymorphism, the response to a function is determined at the compile time itself, unlike Dynamic Polymorphism where it is decided at run-time.
  3. The mechanism of linking a method or function with an object during compile time is called early binding or static binding. 
  4. Using Static Polymorphism, we can perform different tasks with the same method name by passing the different parameters.
  5. In C#, the Static Polymorphism is achieved by using Overloadings.

If you see the IL Code of the overloaded Sum() method, it is bound at compile-time itself based on the types of parameters instead of run-time:


Static-Binding-CSharp




Overloading


  1. The overloading is one of the common ways to implement Static Polymorphism in C#.
  2. If we create two or more members having the same name but different signatures, it is known as member overloading.
  3. The method signature is defined by the method name and the definitions of the method parameters (only parameter types are considered, and the parameter names are ignored).
  4. The returned type as a result of the method is not a part of its signature. If the returned type was a part of the signature, then the compiler doesn't know which method exactly to call (there is ambiguity).
  5. Method Modifiers (static, virtual, sealed, abstract) and Access Modifiers (public, private, internal, protected, etc) are also not part of the method signature.
  6. At compile-time, the compiler works out which one it is going to call, based on the compile-time types of the arguments and the target of the method call.
  7. Generally overloading is used for convenience, usually with all overloads ending up calling one common "master" method name.



Types of Overloading


There are multiple ways to implement Static Polymorphism in C#. Below are the different ways to implement Overloading in C#:

  1. Method Overloading or Function Overloading
  2. Constructor Overloading
  3. Indexer Overloading
  4. Operator Overloading

Examples


 



Method Overloading (Function Overloading)


  1. Method Overloading is the process of creating multiple methods in a class with the same name but with a different signature
  2. Creating a method in the derived class with the same name as a method in the base class but a different signature is also known as Method Overloading.
  3. The advantage of method overloading is that it increases the readability of the program as you do not need to use different method names for the same action.
  4. There are some Rules of Overloading which we have to follow while overloading.

Method-Overloading



Method Overloading Fundamental Rules


Two methods are overloaded only if they have different signatures at compile time. Two method signatures are considered as different only if they have:  

    1. Different number of parameters
    2. Different types of parameters
    3. Different order of parameters
    4. Different kinds of parameters (REF, OUT, IN, Dynamic)

Two methods are not considered as overloaded if they are different only in terms of:  

    1. Different return types (int, string, double, etc.)
    2. Different access modifiers (private, public, protected, internal, etc.)
    3. Different method modifiers ( static, virtualsealedabstract, partial, etc.)  
    4. Different reference parameter modifiers (refoutin)
    5. Different boxing parameter types (object, dynamic)
    6. Different parameter types (params, optional, mandatory)
    7. Overriding Method and Normal Method (Inheritance Based Overloading)

The compiler does not consider the above cases while differentiating the overloaded method. In all the above cases you will get a compile-time error



Method Overloading vs Method Overriding


  1. Method Overloading is static polymorphism while Method Overriding is dynamic polymorphism. So Method Overloading executes at compile-time while Method Overriding executes at run-time.
  2. Method Overloading is generally defined in the same class while Method Overriding only is used in inheritance.
  3. For Method Overriding the base class method must be virtual (abstract in abstract class) while it is not required in the case of Method Overloading.
  4. In Method Overriding you can not change the method signature in the derived class while Method Overloading is only possible with different method signatures.
  5. Method Overriding requires forcefully rewriting the method definition with the same signature in the derived class while Method Overloading is more flexible by allowing different signatures for different overloading methods.
  6. The Overriding Method is not considered as an overloaded member of the derived class, while other methods with the same name and different signatures are considered as the overloaded methods.

Please read more about the method overriding here.



Constructor Overloading


A constructor is nothing but a method without any return type which has the same name as the class or struct name. Constructors are called automatically when we create the object of the class.

Constructor Overloading is quite similar to Method Overloading. It is the ability to define Constructors with different parameters. But static constructors cannot be overloaded because parameterized static constructors are not allowed.

Please read more about the constructors and constructor overloading here.


Constructor-Overloading




Indexer Overloading 


An indexer allows an object to be indexed such as an array.  When you define an indexer for a class or structure, this class or structure behaves similar to a virtual array. So you can access the instance of this class or structure using the array access operator ([ ]).


The indexers can return or set a particular value from the current object instance. So, indexers are not defined with any names, but with the "this" keyword, which refers to the current object instance. 


Like Methods and ConstructorsIndexers can also be overloaded in C#. We can define multiple indexers in a single class or structure


It is not necessary that the indexes have to be only integers. So the indexer may have different types of parameters or numbers of parameters or both.


Indexer-Overloading




Operator Overloading


The concept of overloading a method is also be applied to operators. Operator overloading provides the ability to use the same operator for different purposes.

An operator can be overloaded by defining a function to it. The function of the operator is declared by using the operator keyword.

It is not recommended to overload equality operator (==) on reference types. Please read more here.

Operator Overloading Rules


  1. Only the predefined C# operators can be overloaded. 
  2. The operator keyword must be used to declare an operator. 
  3.  A unary operator has one input parameter. 
  4. A binary operator has two input parameters.
  5. The overloaded function can contain both a public and a static modifier.



Operator-Overloading


Non-Overloadable Operators


  1. The Compound Assignment Operators cannot be overloaded. For example (+=, -=, *=, /=, %=) operators cannot be overloaded.
  2. The Cast Operator cannot be overloaded. For example (T) x.
  3. The Array Index Operator cannot be overloaded but indexers can be defined. For example, a[i] cannot be overloaded.
  4. The Conditional Logical Operators cannot be overloaded directly. For Example (&&, ||) operators cannot be overloaded directly.
  5. The Assignment Operator(=), Dot Operator(.), Ternary Operator(? :), Arrow Operator (->) cannot be overloaded.
  6. The as, await, checked, unchecked, default, delegate, is, nameof, new, sizeof, stackalloc, switch, with, typeof operators cannot be overloaded.

Please read more about operator overloading here.




Live Demo




Conclusion


  1. Overloading is a way to achieve the static polymorphism in C#. 
  2. The compiler decides which method is going to be called at compile time itself in the static polymorphism.
  3. Overloading is what happens when you have two methods with the same name but different signatures. You can overload methods, constructors, indexers, and operators.
  4. We must have to follow the overloading rules while overloading the members.
  5. In the next article, we will walk through the overloading dilemma and ambiguity.



No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.