0
Overloading Methods problem
What is the output of this code? static void Print(int a) { Console.WriteLine(a*a); } static void Print(double a) { Console.WriteLine(a+a); } static void Main(string[] args) { Print(3); } Im am particularity confused with this question because can't 3 pass for an int and a double? I know in some programming languages, this program will cause an error because 3 could pass for an integer or double.
2 odpowiedzi
+ 2
You can wrap the overloads inside a class to make it work like this:
using System;
namespace Testing
{
class OverloadIssue
{
static void Print(int a)
{
Console.WriteLine(a*a);
}
static void Print(double a)
{
Console.WriteLine(a+a);
}
static void Main(string[] args)
{
Print(3);
Print(3.1);
}
}
}
Output:
9
6.2
Hth, cmiiw
+ 2
You can do Print((double) 3);
It explicitly converts 3 into a double before passing it as an argument.