C#.Net find output programs (Method Overloading) | set 1

Find the output of C#.Net programs | Method Overloading | Set 1: Enhance the knowledge of C#.Net Method Overloading concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 09, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        static void Addition(int x, int y)
        {
            int z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        static void Addition(float x, float y)
        {
            float z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            Addition(10, 20);
            Addition(10.4, 20.7);
        }
    }
}

Output:

main.cs(29,13): error CS1502: The best overloaded method match for `Demo.Program.Addition(int, int)' has some invalid arguments
main.cs(7,21): (Location of the symbol related to previous error)
main.cs(29,22): error CS1503: Argument `#1' cannot convert `double' expression to type `int'

Explanation:

The above program will generate syntax errors. In the above program, we overloaded the Addition() method in Program class, It is overloaded for integer arguments and "float" arguments,

Addition(10.4, 20.7);

In the above method call, we passed arguments of type double instead of float or integer that's why the above program generated syntax error.

The correct method call is given below:

Addition(10.4F, 20.7F);

Question 2:

using System;

namespace Demo
{
    class Program
    {
        void Addition(int x, int y)
        {
            int z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        void Addition(float x, float y)
        {
            float z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            Addition(10, 20);
            Addition(10.4F, 20.7F);
        }
    }
}

Output:

main.cs(28,13): error CS0120: An object reference is required to access non-static member `Demo.Program.Addition(int, int)'
main.cs(29,13): error CS0120: An object reference is required to access non-static member `Demo.Program.Addition(float, float)'

Explanation:

The above program will generate syntax errors because we called the non-static method without creating an object inside the static method. Here, we called the Addition() method in the static method Main().

Question 3:

using System;

namespace Demo
{
    class Program
    {
        void Addition(int x, int y)
        {
            int z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        int Addition(int a, int b)
        {
            float z = 0;

            z = a + b;

            Console.WriteLine("Addition is: " + z);

            return 0;
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            Program P = new Program();
            P.Addition(10, 20);
            
        }
    }
}

Output:

main.cs(16,13): error CS0111: A member `Demo.Program.Addition(int, int)' 
is already defined. Rename this member or use different parameter types
main.cs(7,14): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error. In the above program, we created two Addition() methods with the same name and same list of arguments but return type is different. We cannot overload a method only based on the return type.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        void Addition(int x, int y)
        {
            int z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        int Addition(int a, int b, int c)
        {
            float z = 0;

            z = a + b + c;

            Console.WriteLine("Addition is: " + z);

            return 0;
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            Program P = new Program();
            P.Addition(10, 20);
            P.Addition(10, 20,30);
        }
    }
}

Output:

Addition is: 30
Addition is: 60
Press any key to continue . . .

Explanation:

In the above program, we overloaded the Addition() method based on the different number of arguments. In the Main() method, we created the object of Program class and call both overloaded methods that will print the additions of given arguments on the console screen.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        void Addition(int x, double y)
        {
            double z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        void Addition(double x, int y)
        {
            double z = 0;

            z = x + y;

            Console.WriteLine("Addition is: " + z);
        }

        //Entry point of the program
        static void Main(string[] args)
        {
            Program P = new Program();
            P.Addition(10, 20.5);
            P.Addition(20.32, 11);
        }
    }
}

Output:

Addition is: 30.5
Addition is: 31.32
Press any key to continue . . .

Explanation:

In the above program, we overloaded the Addition() method based on a different order of arguments. In the Main() method, we created the object of Program class and call both overloaded methods that will print the additions of given arguments on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.