C# 4: conflicting overloaded methods with optional parameters

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-04-20T10:53:01Z Indexed on 2010/04/20 11:03 UTC
Read the original article Hit count: 385

I have two overloaded methods, one with an optional parameter.

void foo(string a)  { }  
void foo(string a, int b = 0) { }  

now I call:

 foo("abc");

interestingly the first overload is called. why not the second overload with optional value set to zero?

To be honest, I would have expect the compiler to bring an error, at least a warning to avoid unintentional execution of the wrong method.

What's the reason for this behaviour? Why did the C# team define it that way?
Thanks for your opinions!

© Stack Overflow or respective owner

Related posts about c#4.0

Related posts about parameters