Saturday, May 5, 2018

C# : Different Ways of Method can be overloaded

Method overloading is a way to achieve compile time Polymorphism where we can use a method with the same name but different signature, Method overloading is done at compile time and we have multiple way to do that but in all way method name should be same.
  • Number of parameter can be different.
  • Types of parameter can be different.
  • Order of parameters can be different.
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 have two parameter object type and have a same name method with two integer parameter so when we call that method with int value so it’ll call that method have integer parameter instead of object type parameters method.

No comments:

Post a Comment