473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
7 4210
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
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
-----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

"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert | last post by:
I am using the php4-cgi Debian package which is based on PHP 4.3.4. I have problems when running php4 on the command line. If I say something like $ php4 index.cgi a number of variables...
12
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
6
by: Jason | last post by:
I have a function which performs a query and returns a table. The one parameter that can get passed in is a date which defaults to NULL. There is an IF statement in the function that will set the...
1
by: Programmer | last post by:
Hi All Here is my problem I'm using a SQLDataAdapter and DataSet I use the method FillSchema(myDataset, SchemaType.Source) The problem is that when i Check the default Values of the Dataset...
3
by: news.onetel.net.uk | last post by:
I and my friend Karl have spent literally all day trying to find out what is causing my error but we are zapped of any further functionality :) I have a form that adds news records. You select...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
7
by: Gordon Smith | last post by:
I have four (4) ASP.NET Web applications/Web sites on a IIS/6 - Windows Server 2003 production server. 3 of them work fine. I just installed the 4th one and it's Application_Start event is not...
20
by: Phil | last post by:
VB2008 I have a DataGridView with MultiSelect = True and SelectionMode=FullRowSelect. One of the columns is a checkbox column. I have a function that goes through all the selected rows and sets...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.