Why C# doesn't support default values in functions?

  • Thread starter Thread starter Vyssokih Max
  • Start date Start date
V

Vyssokih Max

Hello!

In C++, I can wrote:

void Update(int count = 0)
{...}

and use it without parameters or with one parameter


In C#, I must wrote:

void Update(int count)
{...}

void Update()
{
Update(0);
}

With many similiar functions it's hard to keep code fine and clear.

What is problem to realize such possibility?

With best regards, Vyssokih Max
 
Does
void Update(int count = 0)
allow you to call Update without any parameters like the C# code you posted
does?

If so, then my opinion would be that the EXPLICIT overloads in C# would
actually be what is more "fine and clear". But then, I've only had brief
encounters with C++ so I guess it comes down to what you're used to as usual
in these cases.
 
Default parameters aren't supported by the Common Language Specification. A
..Net language may support them (VB.Net does), but other languages may not be
able to use them. They will have to call the method passing the default
value.
This means that you can write Update(Optional count As Integer=0) in VB.Net,
call it as Update() from VB.Net but you have to call it as Update(0); from
C#.

As for "fine and clear", default parameters can cause problems with
overloaded methods. Suppose you define two methods:
void Update(int count=0){};
void Update(){};
and you make the following call:
myObject.Update();
Which method did you call? The compiler has no way of knowing which method
to call and returns an error.

It is quite possible that default parameters aren't supported by C# because
of the possibility of creating this or similar problems.
 
-----Original Message-----

Default parameters aren't supported by the Common Language Specification. A
..Net language may support them (VB.Net does), but other languages may not be
able to use them. They will have to call the method passing the default
value.
This means that you can write Update(Optional count As Integer=0) in VB.Net,
call it as Update() from VB.Net but you have to call it as Update(0); from
C#.

As for "fine and clear", default parameters can cause problems with
overloaded methods. Suppose you define two methods:
void Update(int count=0){};
void Update(){};
and you make the following call:
myObject.Update();
Which method did you call? The compiler has no way of knowing which method
to call and returns an error.

Yes, it's a *compiler* error as you say. But there's
nothing wrong with the compiler pointing out the error to
you and allowing you to then fix the problem.

I, too, wish that C# had offered the same ability VB has
of named, optional parameters (not the crippled C/C++
method of default parameters). Many other languages offer
the same ability and it does make things easier (IMO) than
having to provide n-overloaded methods with the varying
permutations of possible parameters.

I've just had to start playing with the MS Office Interop
assemblies and it is quite tedious to have to supply
Type.Missing over and over in parameter lists to various
VBA methods!
It is quite possible that default parameters aren't supported by C# because
of the possibility of creating this or similar problems.

or laziness, deadlines, simplicity,... :-)

-- TB
 
Thomas W. Brown said:
Yes, it's a *compiler* error as you say. But there's
nothing wrong with the compiler pointing out the error to
you and allowing you to then fix the problem.

I, too, wish that C# had offered the same ability VB has
of named, optional parameters (not the crippled C/C++
method of default parameters). Many other languages offer
the same ability and it does make things easier (IMO) than
having to provide n-overloaded methods with the varying
permutations of possible parameters.

Too many problems with optional parameters. Most explicitly they don't
version at all and can result in very odd bugs throughout your code.
 
Hello, Daniel!
You wrote on Wed, 12 Nov 2003 19:10:17 -0600:


Too many problems with optional parameters. Most explicitly they don't
version at all and can result in very odd bugs throughout your code.
Can you describe some bugs.
A lot of programmers on C++ live with such ability and don't have a lot of
problems


With best regards, Vyssokih Max. E-mail: (e-mail address removed)
 
Can you describe some bugs.
A lot of programmers on C++ live with such ability and don't have a lot of
problems

One of the problems is that default parameter values are hardcoded into
the calling assembly. If the called assembly (ie the one containing the
method with the optional parameters) changes the values for those
optional parameters, those changes aren't seen until the calling
assembly is recompiled.
 
Jon Skeet said:
One of the problems is that default parameter values are hardcoded into
the calling assembly. If the called assembly (ie the one containing the
method with the optional parameters) changes the values for those
optional parameters, those changes aren't seen until the calling
assembly is recompiled.

Thats the big one. And the "bugs" I was refering to would be any change in
functionality due to that.

However there are a few other problems I had in mind as well. For example,
if default\optional parameters are defined and no one bothers to document
the actual default value(probably assuming the default value is document "in
code"), you could get code thats written in a different language that causes
unexpected results because their assumption of the meaning of default is
actually illegal or does something unexpected(say, Null, when it should have
been Class.EmptyInstance). Even if they are documented, a change ends up
requiring editing the project in those languages in all cases.
Overloads don't completely fix this, a change in default value can still
change behaviour, or the overload could be changed to have a different
meaning, but in properly written cases it shouldn't happen.
As a further annoyance, developers don't bother to provide overloads because
they have optional parameters, resulting in methods with a half dozen to a
dozen arguments that have to be filled out in other languages. That is
really rather annoying to C# developers, ;), well, me atleast.

 
Back
Top