default parameters

  • Thread starter Thread starter Christopher
  • Start date Start date
C

Christopher

One thing that really erks me is the lack of default parameters in C#,
such that I have to write 16 versions of a constructor in C#, one for
every permutation of default params in a C++ constructor, when
migrating. While I know that C# does not support default parameters,
can someone explain the reasoning behind why it does not?
 
One thing that really erks me is the lack of default parameters in C#,
such that I have to write 16 versions of a constructor in C#, one for
every permutation of default params in a C++ constructor, when
migrating.

If you are handling all possible permutations of optional parameters, then
you're going to have almost as many constructors in C++ as you'd required
in C#. Default values don't address permutations...they simply allow you
to leave out the last N parameters for the function.
While I know that C# does not support default parameters,
can someone explain the reasoning behind why it does not?

I can't tell you the actual reason why not. And I admit, the first time I
tried to use a default parameter in C#, I had a "huh?" moment and like
you, felt it was a bit arbitrary to leave that feature out.

However, since that moment, I've done a lot more C# programming and have
come to see C# as a language with relatively simple syntax, with the
simplicity of the syntax being a positive thing. It can be powerful, but
most of that power comes from utilizing the simple parts in powerful ways.

I think it's entirely possible that the C# designers felt that allowing
default values for parameters would overcomplicate the language
unnecessarily. After all, the language needs to support overloading
anyway, and overloading is not only a reasonable alternative to default
values, it allows you do implement more complex default behavior than a
default value would.

Not that supporting default values would prevent you from doing that
otherwise. In fact, I look at default values as being a sort of short-cut
for declaring multiple overloads. It's just that the utility of default
values is relatively minor as compared to the utility of supporting
overloads.

Sometimes I find myself missing default values for parameters. I think to
myself "would it really have been that hard or detrimental to include
this?"

But it's never really that big of a deal. Writing additional overloads
doesn't really create that much overhead. Or put another way, if you've
got a situation where the requirement to write overloads causes you to
have to write 16 _additional_ methods that you wouldn't have otherwise,
there's a really good possibility that your interface has gotten out of
hand and needs to be redesigned anyway. :)

Pete
 
Christopher said:
One thing that really erks me is the lack of default parameters in C#,
such that I have to write 16 versions of a constructor in C#, one for
every permutation of default params in a C++ constructor, when
migrating. While I know that C# does not support default parameters,
can someone explain the reasoning behind why it does not?

http://msdn.microsoft.com/msdntv/transcripts/20040624csharpahTranscript.aspx

<<
Hejlsberg:

Okay. The question is, Why don’t we have default parameters? I think
there are a couple of reasons. First of all, they’re somewhat
duplicative of overloading. I mean you can do the same with
overloading, and which is indeed what people do today, but yes, you’re
right. It’s more typing for sure. I think the other one is subtler, and
in retrospect maybe not as important as I thought. Originally in the
language design are- our issue with default parameters is that they
burned the default into the call site as opposed to leave it up to the
implementation. If you do overloads, let’s say you have void f, and
then, let’s say, you have void f of index, for example, and void f of
index is where the work happens, right? Then in this guy here, you
simply say echo 5, because your default is 5. When the user calls f, it
is up to you to change- you can change what your default is later in v2
of your API. When you have a call f somewhere, this by the compiler is
translated into f of 5 in the call site,-

-because it just copies that value. Now you can never change the
default. This gives you more flexibility, but- and I thought that that
was important. Perhaps it’s not all that important, but-
 
Christopher said:
One thing that really erks me is the lack of default parameters in C#,
such that I have to write 16 versions of a constructor in C#, one for
every permutation of default params in a C++ constructor, when
migrating. While I know that C# does not support default parameters,
can someone explain the reasoning behind why it does not?

When a language is designed, the certain choices has to be made.

This feature was decided not to put in the language.

The links below gives some possible explanations:

http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85556.aspx
http://blogs.msdn.com/brada/archive/2003/11/23/50836.aspx

Arne
 
[...] Originally in the
language design are- our issue with default parameters is that they
burned the default into the call site as opposed to leave it up to the
implementation. If you do overloads, let’s say you have void f, and
then, let’s say, you have void f of index, for example, and void f of
index is where the work happens, right? Then in this guy here, you
simply say echo 5, because your default is 5. When the user calls f, it
is up to you to change- you can change what your default is later in v2
of your API. When you have a call f somewhere, this by the compiler is
translated into f of 5 in the call site,-

-because it just copies that value. Now you can never change the
default. This gives you more flexibility, but- and I thought that that
was important. Perhaps it’s not all that important, but-

Well, that's an interesting perspective (duh :) ). However, I'm not sure
why a default value for a parameter _must_ be "burned into the call
site". I realize that's how it's done in C++, but C# could have instead
used the default value syntax to create implicit overloads. In that way,
the default value would be part of the implementation instead.

Of course, that adds some hidden complexity to the language. It would be
one more thing where C# looks a lot like C++ but then does something not
quite the same as C++, which could lead to confusion. And of course, this
all goes back to the fact that an explicitly written overload does the
same thing, so it's not like functionality is missing.

But that would have been an option, if trying to address that latter point
of his. I'm disappointed he didn't touch on that in his talk.

Pete
 
Every language has its idiosyncrasies, and tradeoffs are made by the language
authors and designers based on sound principals and goals. I don't see the
lack of default parameters in C# being an impediment. Overloads and the
option of using the params keyword with an object array as the last
parameter, combined with some simple handling of same in the method body,
provide quite a bit of flexibility to the programmer while preserving the
goal of simplicity in the language.

I mean, if it's that big of an issue for you, you are still free to use
managed C++.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top