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

Find the output of C#.Net programs | Method Overloading | Set 2: 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
    {
        void Addition(int x, decimal y)
        {
            decimal z = 0;

            z = x + y;

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

        void Addition(int x, double 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(10, 21.5);
        }
    }
}

Output:

@Addition is: 30.5
@Addition is: 31.5
Press any key to continue . . .

Explanation:

In the above program, we overloaded the Addition() method based on different types 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.

P.Addition(10, 20.5);
P.Addition(10, 21.5);

Both the above method call will call the method with the argument of double type because, To call the method with the decimal argument we need to pass the second argument with the suffix 'M', the correct method call with the decimal argument is given below,

P.Addition(10, 21.5M);

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(long x, long y)
        {
            int 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);
            P.Addition(10L, 21L);
        }

    }
}

Output:

main.cs(20,17): error CS0266: Cannot implicitly convert type `long' to `int'. 
An explicit conversion exists (are you missing a cast?)

Explanation:

The above program will generate a compile-time error. In the above program, we overloaded the Addition() method based on different types of arguments. Here, the First method is used integer argument and the second method used arguments of long type.

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

    z = x + y;

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

In the above method, x and y are "long" type but z is "int" type. And the addition of long values cannot be assigned in the "int" variable in C#.

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);
        }

        void Addition(short x, short y)
        {
            short 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);
            P.Addition(10H, 21H);
        }

    }
}

Output:

main.cs(30,25): error CS1525: Unexpected symbol `H'

Explanation:

The above program will generate compile-time errors. In the above program, we overloaded the Addition() method based on different types of arguments. Here, the First method is used integer argument and the second method used arguments of short type.

P.Addition(10H, 21H);

The above method call used 'H' as a suffix but it is not valid that's why compile-time errors are generated.

Question 4:

using System;

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

            z = x + y;

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

    class B:A
    {
        public void Addition(float x, float y)
        {
            float z = 0;

            z = x + y;

            Console.WriteLine("B: Addition is: " + z);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            B X = new B();
            X.Addition(10, 20);
            X.Addition(10.25F, 11.25F);
        }

    }
}

Output:

B: Addition is: 30
B: Addition is: 21.5
Press any key to continue . . .

Explanation:

In the above program, we created three classes A, B, and Program. Both A and B class contains the Addition() method with different types of arguments. Here, class A is inherited in class B.

B X = new B();
X.Addition(10, 20);
X.Addition(10.25F, 11.25F);

In the Main() method of Program class we created an object of class B. Here, both above method calls will execute Addition() method of class B.

Question 5:

using System;

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

            z = x + y;

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

    class B:A
    {
        public void Addition(float x, float y)
        {
            float z = 0;

            z = x + y;

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

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            A X = new A();
         
            X.Addition(10, 20);
            X.Addition(10.25F, 11.25F);
        }
    }
}

Output:

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

Explanation:

The above program will generate compile-time errors. In the above program, we created three class A, B, and Program. Both A and B class contains the Addition() method with different types of argument.

Here class A is inherited in class B.

In the Main() method of Program class we created an object of class A. But the Addition() method of the class A can accept integer arguments only. But here we called a method with float arguments that's why compile-time errors will be generated by the above program.





Comments and Discussions!

Load comments ↻





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