views:

239

answers:

4

Hello, 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?

+6  A: 

i think it is useless to try to answer question "Why?". But i have good news, C# 4.0 has them.

Andrey
oooh, wasn't aware. I look forward to it! :)
Earlz
On the other hand, C# 4.0 has them for easier interoperability, not for general purpose - most people will not understand that and just use them for everything. I don't think this is good news.
OregonGhost
+1  A: 

Optional arguments are not that simple as they look like. You might get complex problems if you have overloaded methods which all have optional arguments. The resolution rules can become very complicated very quickly. I guess for the first version of C# there was not good solution, so they skipped the optional parameters.

But you probably know that C# 4.0 will have optional and named arguments, so it seems not to be a bad idea in general! ;-)

Achim
+12  A: 

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 const values from C# 1.

Jon Skeet
Agree with this one. The problem is that the problems with default values are not understood by the majority of the developers using it, getting complicated when the assembly with the optional parameter is updated with a *different default value*. On the other hand, named arguments are a good argument to have optional parameters...
OregonGhost
I don't buy that argument against optional parameters. When changing a public API the users will have to change their code to react to the different behavior regardless if you're using method overloading or optional parameters.
ChaosPandion
@ChaosPandion: Whether you buy it or not, that's the reason Anders has given before now.
Jon Skeet
I don't quite understand your response.
ChaosPandion
@ChaosPandion: You said you didn't "buy" that argument - I'm saying it's the argument given by the language designer, so it's the reason C# hasn't had optional parameters before, whether or not you (or I) agree with the reason.
Jon Skeet
Woo! Jon Skeet answered one of my questions!!
Earlz
Oh OK, when I said "buy" I meant "think it is a valid" rather than "think that is the reason".
ChaosPandion
@Earlz - Jon has answered 7370 questions. The odds are strong that he will answer one of yours at some point. :)
ChaosPandion
**Sam Ng's blog has some interesting discussion of this:** *"Try as we might, people still use it [COM] (and are still going to continue using it). What does COM have to do with C#? Office. Office PIAs. They are designed such that many of methods have about 30 parameters, and all are optional. Most of the time, what you’d want is to specify one argument, and use defaults for the rest. Enter named and optional arguments. Because we allow you now to call methods without specifying optional arguments, you can now call these Office methods without passing Type.Missing for every other argument."*
LBushkin
+6  A: 

Its not there yet, but it is in C# 4. This is largely to do with cost, and how well the feature fits in with the major new parts of the language (such as Generics in .Net 2, or linq in 3). Optional arguments fit will with the new dynamic stuff coming in version 4, so have been included.

To quote Eric Lippert (Who was himself quoting Eric Gunnerson) on why many seemingly good features are not included:

(1) this is not a subtractive process; we don't start with C++ or Java or Haskell and then decide whether to leave some feature of them out. And (2) just being a good feature is not enough. Features have to be so compelling that they are worth the enormous dollar costs of designing, implementing, testing, documenting and shipping the feature. They have to be worth the cost of complicating the language and making it more difficult to design other features in the future.

Jack Ryan
+1 for the increased complexity of fitting in other features in the future, even though I like default arguments.
dsimcha