Q:

C# program to demonstrate method overloading based on the order of arguments

0

C# program to demonstrate method overloading based on the order of arguments

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to demonstrate method overloading based on the order of arguments is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate method overloading based 
//on the order of arguments

using System;

class MethodOver
{
    static double Sum(int a, int b)
    {
        float r = 0;

        r = a + b;
        return r;
    }
    static double Sum(int a, float b)
    {
        float r = 0;

        r = a + b;
        return r;
    }

    static double Sum(float a, int b)
    {
        float r = 0;

        r = a + b;
        return r;
    }
    static void Main(string[] args)
    {
        double result = 0;

        result = Sum(10, 20);
        Console.WriteLine("Sum : " + result);

        result = Sum(10, 20.24F);
        Console.WriteLine("Sum : " + result);

        result = Sum(27.38F, 30);
        Console.WriteLine("Sum : " + result);
    }
}

Output:

Sum : 30
Sum : 30.2399997711182
Sum : 57.379997253418
Press any key to continue . . .

Explanation:

In the above program, we created a class MethodOver, here we overloaded the sum() method based on the order of arguments to calculate the sum of given arguments.

Here, we created the three methods to calculate the sum of given arguments and return the result to the calling method.

Now look to the Main() method. Here, we created the local variable result and then called each overloaded method one by one and printed the result on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the constructor overload... >>
<< C# program to demonstrate method overloading based...