c# 4.0 - Conflicting overloaded methods with optional parameters -


i have 2 overloaded methods, 1 optional parameter.

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

now call:

 foo("abc"); 

interestingly first overload called. why not second overload optional value set zero?

to honest, have expect compiler bring error, @ least warning avoid unintentional execution of wrong method.

what's reason behaviour? why did c# team define way?

from msdn:

if 2 candidates judged equally good, preference goes candidate not have optional parameters arguments omitted in call. consequence of general preference in overload resolution candidates have fewer parameters.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

What is the difference between data design and data model(ERD) -

ios - Can NSManagedObject conform to NSCoding -