C Sharp Member Overloading

C# Member Overloading

Member overloading in C# is the process of creating two or more members having the same name but they are different in number or type of parameters. Methods, constructors, and indexed properties have parameters only and thus can be overloaded in C#.

C# Method Overloading:

Method overloading in C# is the process of creating two or more methods having the same name but they are different in the parameter. Thus, there is no need to use different names for the same action and increases the readability of the program. Method overloading in C# in two ways, either by modifying the number of arguments or by modifying the data type of the arguments.

Example 1:

using System;  
public class maths{  
    public static int mul(int x,int y){  
        return x * y;  
    }  
    public static int mul(int x, int y, int z)  
    {  
        return x * y * z;  
    }  
}  
public class values  
{  
    public static void Main()  
    {  
        Console.WriteLine(maths.mul(10, 20));  
        Console.WriteLine(maths.mul(10, 20, 30));  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of method overloading in C#. Here, the number of arguments of the mul() method is changed. Both the values, before the change and after the change, are displayed on the screen.

Example 2:

using System;  
public class maths{  
    public static int mul(int x,int y){  
        return x * y;  
    }  
    public static float mul(float x,float y){  
        return x * y;  
    }  
}  
public class values  
{  
    public static void Main()  
    {  
        Console.WriteLine(maths.mul(10, 20));  
        Console.WriteLine(maths.mul(10.1f, 20.2f));  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of method overloading in C#. Here, the data type of arguments is changed. Both the values, before the change and after the change, are displayed on the screen.

Please Share