Whats wrong with this code? | Sololearn: Learn to code for FREE!
+ 1

Whats wrong with this code?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { static int Sum(int a, int b){ return a +b; } static int Sum(int a, int b, int c=2){ return a + b + c; } //If we now write this in main: int a; a = Sum(4,5); //It gives some errors ( for e.g it says static is not valid) } } } //It gives some errors ( for e.g it says static is not valid) //Also will it give '9' or '11' and why?

3rd Feb 2019, 8:23 AM
Raptor Bot
Raptor Bot - avatar
1 Answer
+ 6
You are writing the static Sum methods inside the Main method, if you move the Sum methods outside it can run just fine. Another thing, I think giving a default value for <c> parameter in the Sum overload is ambiguous, it's confusing which method to invoke when two arguments were passed, because that <c> parameter have default value IMHO : )
3rd Feb 2019, 8:58 AM
Ipang