What are Different Ways a Method can be Overloaded?

Category: C SharpWhat are Different Ways a Method can be Overloaded?
Editor">Editor Staff asked 2 years ago

What are Different Ways a Method can be Overloaded?

1 Answers
Editor">Editor Staff answered 2 years ago

Method overloading is a way to achieve compile-time polymorphism where we can use a method with the same name but different signatures. For example, the following code example has a method volume with three different signatures based on the number and type of parameters and return values.
 
Example

 

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5.     
  6. namespace Hello_Word {    
  7.     class overloding {    
  8.         public static void Main() {    
  9.             Console.WriteLine(volume(10));    
  10.             Console.WriteLine(volume(2.5F, 8));    
  11.             Console.WriteLine(volume(100L, 75, 15));    
  12.             Console.ReadLine();    
  13.         }    
  14.     
  15.         static int volume(int x) {    
  16.             return (x * x * x);    
  17.         }    
  18.     
  19.         static double volume(float r, int h) {    
  20.             return (3.14 * r * r * h);    
  21.         }    
  22.     
  23.         static long volume(long l, int b, int h) {    
  24.             return (l * b * h);    
  25.         }    
  26.     }    
  27. }    

Note 
If we have a method that has two parameter object type and has the same name method with two integer parameters, when we call that method with int value, it will call that method with integer parameters instead of the object type parameters method.