No default parameters in C#

A

Alain Dekker

Hi,

I know that C# has not allowed the use of default parameters. So this
function prototype (which works nicely in C++) does not compile in C#:

void DoMyWork(bool bLoggedOn, int nAccessLevel = 0);

Briefly, what is the rationale behind not allowing default parameters? One
nice feature you can achieve with default parameters in C++ is recursive
functions as in:

void DoMyWork(bool bLoggedOn, int nLevel = 0)
{
if ((bLoggedOn) && (nLevel < 3))
{
nLevel++;
DoMyWork(bLoggedOn, nLevel);
}
else
{
// Do something when nLevel == 3?
}
}

I guess in C# you would do this using function overloads?

void DoMyWork(bool bLoggedOn, int nLevel)
{
// blah
}

void DoMyWork(bool bLoggedOn)
{
DoMyWork(bLoggedOn, 0);
}

Thanks,
Alain
 
A

Alain Dekker

Found this thread which explains the original reasons for not including
default parameters (which, in my opinion along with most of the commentors,
was a silly argument):

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85556.aspx

I then followed a link in a more recent comment that shows that starting
from C# 4.0, MS have allowed default parameters (which they call Optional):
http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx

It seems from the threads that Microsoft hadn't completed the complex task
of getting Intellisense and other documentation to work with default
parameters so sent out one of their big guns to sell the lack of this
feature to programmers. Now that they have finally completed the work, they
have quietly included it in later IDEs.

Thanks,
Alain
 
J

James A. Fortune

Found this thread which explains the original reasons for not including
default parameters (which, in my opinion along with most of the commentors,
was a silly argument):

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85556.aspx

I then followed a link in a more recent comment that shows that starting
from C# 4.0, MS have allowed default parameters (which they call Optional):http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx

It seems from the threads that Microsoft hadn't completed the complex task
of getting Intellisense and other documentation to work with default
parameters so sent out one of their big guns to sell the lack of this
feature to programmers. Now that they have finally completed the work, they
have quietly included it in later IDEs.

LOL. Thanks for the links and the commentary. In, say, VBA, Optional
Parameters are very convenient because they allow the programmer to
expand a function without taking much of a chance of breaking prior
code. Please excuse the following VBA code portion used to illustrate
Optional Parameter usage there:

VBA Declaration:
Public Function IsFieldInTableDef(strFieldName As String, strTableName
As String) As Boolean

After a while, the function is used several times in several
databases. A new database comes along where I discover that I need to
hook up to a table in a remote database dynamically. By using an
Optional Parameter, I don't have to write a separate function to allow
that functionality by any of the original function calls, plus I know
that if the Optional Parameter is not used, the function will behave
as it did before:

New VBA Declaration:
Public Function IsFieldInTableDef(strFieldName As String, strTableName
As String, Optional strDatabase As String) As Boolean

I really like the convenience afforded by Optional Parameters in VBA,
but it seems that C#'s way of rolling all the Optional Parameters into
a larger signature (Note: overloading is not available in VBA) negates
the benefits allowed in VBA unless a wrapper is used to map the
original signature into the larger. In managing a software project
over time, maybe several wrappers might sprout up, with a new wrapper
each time a set of new arguments get added to a function. The new
Optional Parameter feature in C# does not appear to be what I hoped it
would be, yet I still welcome its addition and hope that it will yet
fulfill some of the purpose for which it is used in VBA.

James A. Fortune
(e-mail address removed)
 
A

Alain Dekker

Hi Peter,

You characterise me unfairly...I'm growing to appreciate C# and .NET ! :blush:)

I'm simply stuck with VS 2005 because of the older target device I need to
support (and which internet searches show is NOT supported in VS 2010).
Might be an opportunity to get my company to upgrade to a more modern mobile
platform...who knows? VS 2005 and Windows CE development is PAINFULLY slow.
Coming from a C++ Native programming in VS 2003 and Delphi Native
programming (where development is rapid), this more than anything else, is
irritating.

I had spotted that the main reason for rolling out Optional parameters was
for Microsoft Office automation, thanks.

As for my tongue-in-cheek comment about the lack of Optional parameters
being a marketing ploy...come on! You read the threads and the majority
(maybe not an overwhelming, but a majority) of developers commenting on this
thread think Optional / Default parameters are a good idea. There are
arguments both for and against. Microsoft themselves have decided that
Optional parameters are useful, even if only because another important part
of their own organisation, the Office team, have included APIs that need
them to make code usable. Several developers gave up on C# and switched to
VB because of the lack of Optional/Default parameters! You might as well
sling mud at the Office team :blush:)

As for working on the language specification, I am not qualified for that
sort of thing (you will surely have guessed this). I have a background in
Chemistry and Physics and am self-taught when it comes to computer
programming. This certainly explains my occasional "wooly thinking". You are
quite wrong if you think me a fool, though :blush:) [I know you do not]

I really appreciate your comments, and thank you for them. For the record,
while I still find C++ syntax more aesthetically pleasing, I am coming to
appreciate C# and .NET and the effort the MS teams have put into the
language. The world would be a dull place if we all thought MS was perfect;
disagreement and debate, in my opinion, are key tools for learning and
innovation.

Regards,
Alain
 

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

Top