4 ms·
Even worse, optional parameters are implemented backwards. That is: public void DoSomething(string required, string optional = null) is actually DoSomething(st
by sitharus 13y ago
Even worse, optional parameters are implemented backwards.
That is: public void DoSomething(string required, string optional = null) is actually DoSomething(string, string), but at compile time a call to DoSomething("foo") is compiled to DoSomething("foo", null).
Hence optional parameters are not recommended for an external API.
- sukuriant 13y agoSo you need to be mindful and have intelligent overloads for the DoSomething method? Personally, I don't really see that as much of a problem. Having run into that issue before, I think the compiler catches it as as an error because its an ambiguous method signature. Instead, you have to pass something like: "DoSomething("foo", (string)null)" which, in that case, defeats the purpose of the optional parameter altogether.
- sitharus 13y agoYeah, been through that before. We just standardised on using overloads for external APIs, it's cleaner externally. The real issue is if you have DoSomething(string what, autoRetry=true) and you change to DoSomething(string what, autoRetry=false) - DLLs compiled against the old version will call with true even if the authors wanted the default behaviour.
- sukuriant 13y agoThat makes sense. As I said in my other thread, I locally deal with that problem by passing null instead of a primitive (though admittedly, I would probably use a primitive in cases of bool). That said, in this circumstance my method sounds more like a hack and yours the cleaner, correct route.