By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

"Vyssokih Max" <fo***@incap.ru> wrote in message
news:OD*************@tk2msftngp13.phx.gbl... 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

Nov 15 '05 #2

P: n/a
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.

"Vyssokih Max" <fo***@incap.ru> wrote in message
news:OD*************@tk2msftngp13.phx.gbl...
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

Nov 15 '05 #3

P: n/a
-----Original Message-----
"Vyssokih Max" <fo***@incap.ru> wrote in message
news:OD*************@tk2msftngp13.phx.gbl...
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

Default parameters aren't supported by the Common

Language Specification. A..Net language may support them (VB.Net does), but other languages may not beable to use them. They will have to call the method passing the defaultvalue.
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); fromC#.

As for "fine and clear", default parameters can cause problems withoverloaded 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 methodto 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# becauseof the possibility of creating this or similar problems.


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

-- TB

Nov 15 '05 #4

P: n/a

"Thomas W. Brown" <tb*********@kaxy.NOSPAM.com> wrote in message
news:26****************************@phx.gbl...
-----Original Message-----
"Vyssokih Max" <fo***@incap.ru> wrote in message
news:OD*************@tk2msftngp13.phx.gbl...
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

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.


Too many problems with optional parameters. Most explicitly they don't
version at all and can result in very odd bugs throughout your code.
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

Nov 15 '05 #5

P: n/a
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: fo***@incap.ru
Nov 15 '05 #6

P: n/a
Vyssokih Max <fo***@incap.ru> wrote:
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


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Vyssokih Max <fo***@incap.ru> wrote:
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


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.