I long for those sweet optional arguments from the days when I programmed in C++ more. I know they don’t exist in C#, but my question is Why.
I think method overloading is a poor substitute which makes things messy very quickly.
void foo(int x,int y,int z=0){
//do stuff...
}
//is so much more clean than
void foo(int x,int y){
foo(x,y,0);
}
void foo(int x,int y,int z){
//do stuff
}
I just do not understand what the reasoning is. The C# compiler would obviously not have a problem supporting this just Microsoft elected not to support it.
Why, when C# was designed, did they not want to support optional arguments?
As Andrey says, C# 4 has optional parameters and named arguments. However, it’s worth pointing out that one of the concerns which made Anders reluctant to include them to start with – namely that the default value (which has to be a constant) gets baked into the calling code – is still present. In other words, it’s the same problem as publicly accessible
constvalues from C# 1.