Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C#: Why default parameters?

2 views
Skip to first unread message

Jonathan

unread,
Jan 22, 2003, 3:58:28 PM1/22/03
to
Dear Microsoft C# Team,

Clearly, Microsoft has good intentions by not including the ability to have
default parameters in the C# language ... at least based on the
justification provided below by Microsoft Developer Support. However, it
seems to be better "style" to inform developers of the impact of one's
decision instead of removing the choice entirely.

I do appreciate that Microsoft has raised an issue that many developers have
probably not realized. That is, that the default parameter value becomes
part of the interface. The significance of that fact does not imply that
default parameters are not good design. To the contrary, the clunky
approaches that developers (and several C# publications) have advised are a
far worse outcome than if default parameters were used.

While the work-around suggested by Microsoft does not cause end-users of
such classes to re-compile, it instead pretends that the "default value"
chosen is un-important to the end-user (by removing it from the interface).
If an end-user of this class were to assume a certain default value and it
was later changed by the developer, then surely the end-user *should* be
notified. The worse part about this method, besides increasing the code
burden, is how it can clutter the interface with a huge set of needless
signatures.

A second approach commonly suggested is the use of a "params[]" object.
This technique is used frequently in un-typed languages such as JavaScript.
However, this design choice removes the ability to cleanly type one's
parameters and also prevents the interface from advertising the default
values chosen by the class implementation.

The lack of default parameters, a relatively modern language feature, is
just silly. The removal of this feature (from the languages in which C# is
based) is indeed a bad design choice. It is rather strange that a language
that gives developers the choice to use the "goto" statement does not give
developers the choice to publish and use default parameter values with their
class interface.

Please consider these thought in future updates to the C# language,
Jonathan


----------------------------------------------------------------------------
From: Indira Dhingra (indira...@microsoft.com)
Subject: RE: Default parameters in C#
Date: 2002-03-12 08:57:31 PST

> C# does not support default parameter because the values do not version
> well. Because default values are compiled into client's code, if
> implementor wants to change the default values, all clients must be
> recompiled.
>
> However, you can probably do what you want by overloading:
>
> class Foo {
> public int Method1 () { Method1 (1, 0); }
> public int Method1 (int a) { Method1 (a, 0); }
> public int Method1 (int a, int b) {
> // do something with a and b
> }
> }
>
> This generates the equivalent of Foo.Method1(int a=1, int b=0) { }
>
> Thanks,
> Indira Dhingra
> Microsoft Developer Support


Alvin Bruney

unread,
Jan 22, 2003, 4:29:56 PM1/22/03
to
I concur. I've had to find other ways to accomplish this, it would have been
nice to have it in there.

"Jonathan" <jona...@NO.SPAM.com> wrote in message
news:uIy74jlwCHA.1624@TK2MSFTNGP10...

Chris A R

unread,
Jan 22, 2003, 5:46:22 PM1/22/03
to
I don't miss default parameters at all. And yes, I do use
overloaded methods. I can recall one I wrote that had about 8
overloads, but I have no trouble with that. It's easy for all the
overloads to just call the one method that accepts all parameters.
Also, it makes sense when I write a method that takes the first, third,
and fifth parameters of the fully loaded version.

Chris A. R.

"Alvin Bruney" <vapo...@hotmail.com> wrote in message
news:OijHj1lwCHA.2352@TK2MSFTNGP09...

Simon Trew

unread,
Jan 22, 2003, 6:39:49 PM1/22/03
to
Mee too. And of course the default value is part of the interface.

Alvin Bruney <vapo...@hotmail.com> wrote in message
news:OijHj1lwCHA.2352@TK2MSFTNGP09...

Stefan Ruck

unread,
Jan 23, 2003, 9:42:44 AM1/23/03
to

Jonathan,

Over all, I do also like (and use) default parameters in
C++. But there is one (or perhaps some more) Guru of the
Week issue (can't recall the number, see
http://www.gotw.ca/gotw/) where one question was, which
parameter value is used when having an object hierarchy
and a pointer of base class type pointing to an object of
a derived class type, where both base and derived class
have a different default value for the same parameter.

It was really funny (and little confusing) to see what
might happen. So one reason might have been to avoid
confusion when having such a scenario in C#.

I'm not sure if this was subject of any justification
provided by MS, but I can imagine it might also has been a
reason.

Stefan

Jonathan

unread,
Jan 23, 2003, 10:22:29 AM1/23/03
to

Stefan,

I appreciate your response and help in trying to provide a reasonable
explanation for Microsoft's decision to leave out this commonly used
language feature. The fact is that many constructs of most any language can
lead to bad design and unplanned outcomes. However, such a scenario as you
describe should simply have a defined outcome so that a design can be left
to the developers and not forced upon them by the language. Further, this
issue has never seemed to be that big of a problem for other languages that
provide this feature .. at least not nearly as big of a problem as leaving
out this feature is causing.

-Jonathan

------------------------------

0 new messages