Method Overloading In C#
In C#, It allows us to create more than one method with the same name, but with different parameter lists and different definitions which is called Method Overloading. It is used when methods are required to perform similar tasks but use different input parameters. It involves the following steps:
1. The compiler tries to find an exact match in which the types of actual parameters are the same and uses that method.
2. If the exact match is not found, then the compiler tries to use the implicit conversions to the actual arguments and then uses the method whose match is unique. If the conversion creates multiple matches, then the compiler will generate an error message.
Program:
using System; class Test { public static void Main() { Console.WriteLine(volume(20)); Console.WriteLine(volume(2.5F, 8)); Console.WriteLine(volume(100L, 75, 15)); } static int volume(int x) { return(x*x*x); } static double volume(float r, int h) { return(3.14519*r*r*h); } static long volume(long l, int b, int h) { return(l*b*h); } }
Output:
8000
157.2595
112500