Overloading Methods problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

27th Sep 2017, 2:41 AM
Benjamin Keurian
Benjamin Keurian - avatar
2 Answers
+ 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
27th Sep 2017, 3:33 AM
Ipang
+ 2
You can do Print((double) 3); It explicitly converts 3 into a double before passing it as an argument.
27th Sep 2017, 5:28 AM
Wen Qin
Wen Qin - avatar