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.
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
Answer 1:
Output:
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);
Answer 2:
Output:
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().
Answer 3:
Output:
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.
Answer 4:
Output:
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.
Answer 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer