A
ad
Does C#2.0 support optional parameters like VB.NET:
Function MyFunction(Optional ByVal isCenter As Boolean = False)
Function MyFunction(Optional ByVal isCenter As Boolean = False)
I certainly hope notad said:Does C#2.0 support optional parameters like VB.NET:
Function MyFunction(Optional ByVal isCenter As Boolean = False)
I certainly hope not![]()
I cant recall seeing a case where I would consider optional parametersad said:Why,
Is there some drawback about optional parameters?
Optional parameter is supported by Delphi(Object Pascal) and VB.NET
Why C# ?
They may well have their place but I have yet to come across a scenarioMark said:Limit them to a minimum yes, but they still have their place. Consider this.
public void Show(params string[] message)
{
if (message.Length == 0)
{
Console.WriteLine("No message!");
return;
}
Console.WriteLine("Messages...");
foreach (string s in message)
{
Console.WriteLine(s);
}
}
public void Show2()
{
Console.WriteLine("No message!");
}
public void Show2(string message)
{
Console.WriteLine("Messages...");
Console.WriteLine(message);
}
public void Show2(string[] message)
{
Console.WriteLine("Messages...");
foreach (string s in message)
{
Console.WriteLine(s);
}
}
Now in this scenario, where we want to pass 0 to many messages in a
parameter for overloading we have had to create 3 different type signatures
wheras using optional params we only need the one. Also note that the
overloading scenario would look even more clumsy if we did checking for zero
length arrays in there as well -since the behaviour of it at present is not
identical to the optional one. Also note for the Show2, if you want to pass
multiple strings, type checking forces you to pass an array, wheras the
optional Show method allows for a list (or an array).
e.g.
inst.Show2(new string[]{"one","two"});
inst.Show("one","two"); //this I believe is the greatest benefit
to the optional parameters.
inst.Show(new string[]{"one","two"});
Br,
Mark.
I cant recall seeing a case where I would consider optional parameters to
be *better than overloading.
To take your case below, we could refactor as:
public bool MyFunction()
{
return Myfunction(false)
}
public bool MyFunction(bool center)
{
//do the dance
}
JB
*better being defined as readibility, ease of use, etc..
John B said:They may well have their place but I have yet to come across a scenarioMark said:Limit them to a minimum yes, but they still have their place. Consider
this.
where I would need them.
Not being a real example to me I can only try and interpret what you are
trying to achieve (I understand the code but would need a real life
situation where it would be applicable to me) but I would possibly write
this as:
public void show()
{
show("No Message");
}
public void show(params string[] messages)
{
foreach(string message in messages)
{
show(message);
}
}
public void show(string message)
{
console.writeline(message);
}
If we really wanted the line:
..WriteLine("Messages...");
then I would probably put a private writeheader method which would be
called by the relevant methods.
JB
public void Show(params string[] message)
{
if (message.Length == 0)
{
Console.WriteLine("No message!");
return;
}
Console.WriteLine("Messages...");
foreach (string s in message)
{
Console.WriteLine(s);
}
}
public void Show2()
{
Console.WriteLine("No message!");
}
public void Show2(string message)
{
Console.WriteLine("Messages...");
Console.WriteLine(message);
}
public void Show2(string[] message)
{
Console.WriteLine("Messages...");
foreach (string s in message)
{
Console.WriteLine(s);
}
}
Now in this scenario, where we want to pass 0 to many messages in a
parameter for overloading we have had to create 3 different type
signatures wheras using optional params we only need the one. Also note
that the overloading scenario would look even more clumsy if we did
checking for zero length arrays in there as well -since the behaviour of
it at present is not identical to the optional one. Also note for the
Show2, if you want to pass multiple strings, type checking forces you to
pass an array, wheras the optional Show method allows for a list (or an
array).
e.g.
inst.Show2(new string[]{"one","two"});
inst.Show("one","two"); //this I believe is the greatest
benefit to the optional parameters.
inst.Show(new string[]{"one","two"});
Br,
Mark.
ad wrote:
Why,
Is there some drawback about optional parameters?
Optional parameter is supported by Delphi(Object Pascal) and VB.NET
Why C# ?
I certainly hope not
I cant recall seeing a case where I would consider optional parameters to
be *better than overloading.
To take your case below, we could refactor as:
public bool MyFunction()
{
return Myfunction(false)
}
public bool MyFunction(bool center)
{
//do the dance
}
JB
*better being defined as readibility, ease of use, etc..
"John B" <[email protected]>
???????:[email protected]...
ad wrote:
Does C#2.0 support optional parameters like VB.NET:
Function MyFunction(Optional ByVal isCenter As Boolean = False)
I certainly hope not
JB
I did not say that they were the root of all evil. What I did say andMark said:I agree to an extent with what you say although I have still used them
occasionally.
Still handled by the private header method.Couple of points though ...
1. What if that extra "Messages..." line was something useful (and many
lines of code)?
true, easily enough fixed by:1.a Firstly your version fails to account for passing an empty array into
that overload. i.e. The "No Message" message would not be printed from your
code.
Not being an example that has any context to me I can only speculate but1.b You mention this extra private method for the header, but have you
thought what it would do to the code? This would have to be called by each
overload apart from the first, which would mean that when the Show(array
sig) method calls the Show(string sig) method you'd get (if a 3 element
array was passed)...
Messages...
Messages...
....
Messages...
....
Messages...
...
2. I still fail to see any benefits to your code in this example over the
optional params. Or to put it another way, what benefit are we getting? If
you write your code so that it displays exactly the same functionality you
will see that it cannot be done as efficiently without using the optional
parameter.
3. Real world example or not, the behaviour that is demonstrated (namely the
fact that we can pass 0 to many arguments and handle it fine in a few lines
of code) can be applied to many many different scenarios.
...I'll give you one. I have a method which takes zero to many attributes
(objects) and adds them into an element object. I want to be able to have
the following to be possible.(apologies for any typos! but you get the
idea)..
I dont think that this would be possible with optional in the vb6/vb.netElement e = new Element();
e.Add();
e.Add(new Attribute());
e.Add(new Attribute(), new Attribute());
e.Add(new Attribute[]{});
e.Add(new Attribute[]{new Attribute(), new Attribute});
Ah, yes, the fanatics out there4. What we have here is the same sort of argument that people say against
jumping execution e.g. GOTO. Those people say NEVER use it. Sometimes
though, certain things are more appropriate to certain scenarios and goto
and optional parameters definitely have a time and place.
Agreed, however, tools like optional params provide an easy way out ofI'll reiterate on thing though and that is to ONLY use them when appropriate
and that refactoring using overloads is most likely going to be appropriate
in 98% of the time.
throw new argumentoutofrangeexception("Oi peanut! Dont pass stupidOddball said:Would this help - I'm not very good at this but gimme a chance:
public void WriteOutput(object input)
{
System.Console.WriteLine("Messages...");
if (input is string[])
{
// Do string array stuff
//including checking for no message
}
else if (input is string)
{
// Do string stuff
// Including once again, checking for no message
}
else
{
// Do what you do to stupid users who pass silly params
Yeah, not what we want in this case as we do want to limit the types}
}
That should let you use either your string array OR your string.. or a Haddock object if
you want without any overloading, optional parameters or jiggery pokery.
Me too (evolving (devolving probably according to some) vb6 monkey)It just uses reflection instead :S But no one is perfect.
The thing with optional parameters vs. overloading is that neither side can win because
both sides have a valid argument. Lazy VB monkeys say optional is easier, anal C#
monkeys say overloading is more structured.
Agreed.
I'm a C# monkey - and was therefor appaled at the suggestion that a method signature
could be so contaminated.... but that's life![]()
John B said:throw new argumentoutofrangeexception("Oi peanut! Dont pass stupidOddball said:Would this help - I'm not very good at this but gimme a chance:
public void WriteOutput(object input)
{
System.Console.WriteLine("Messages...");
if (input is string[])
{
// Do string array stuff
//including checking for no message
}
else if (input is string)
{
// Do string stuff
// Including once again, checking for no message
}
else
{
// Do what you do to stupid users who pass silly params
parameters"); //hehe
Yeah, not what we want in this case as we do want to limit the types that}
}
That should let you use either your string array OR your string.. or a
Haddock object if you want without any overloading, optional parameters
or jiggery pokery.
can be passed but does the job.
Me too (evolving (devolving probably according to some) vb6 monkey)It just uses reflection instead :S But no one is perfect.
The thing with optional parameters vs. overloading is that neither side
can win because both sides have a valid argument. Lazy VB monkeys say
optional is easier, anal C# monkeys say overloading is more structured.
Agreed.
I'm a C# monkey - and was therefor appaled at the suggestion that a
method signature could be so contaminated.... but that's life![]()
Of course they do have their place and would be valuable in some cases.
Doesnt mean I miss them or want them or would be happy if c# supported
them.
I cannot _win_ this discussion, nor do I really want to, but if anyone who
queries this question reads this discussion, they might well think a bit
about why they think they need them and can maybe come to a "better"
solution. ("better" by my definition, one that does not need optional
params)
JB------------------------------------
Another unchecked rambeling brought to you by:
Oddball
joshua@bf#N0SP4M#wd.co.uk
The point was to illustrate an easy/neat way to get around not having...and finally
John, even your overloaded solution uses the optional params statement
thereby contradicting your entire argument and making your code rewrite
pointless (also let me remind you of my original [nice compact and fully
functional] code)
public void Show(params string[] message){
if (message.Length == 0){
Console.WriteLine("No message!");
return;
}
Console.WriteLine("Messages...");
foreach (string s in message) {
Console.WriteLine(s);
}
}
OMGHow on earth you can think that your refactored solution is an improvement
over this leaves me scratching my head.
Not what I would call nice, but I would not say horrible, matter ofEven if you are relatively new to C#
you would surely see how horrible your refactored code is?
Note the line in
your first post "*better being defined as readibility, ease of use, etc..",
well I disagree with every point in this sentence with regards to the
context of the examples I have given.
*Oddball* yeah fine but...
1. You are sacrificing the ability to have the compiler check the types
passed to the procedure. Why provide the ability to pass all types
(including Haddock objectswhen we only want strings and empty in this
example. Typechecking is there for a reason.
Maybe you missed or misread the lineIt was interesting to note that
John didn't decide this was a retrograde step.
Gee, so you are saying I _could_ recall seeing a case where I would2. Testing the types within the method is *not* (as I'm sure you are aware)
an improvement over my code (in any respect e.g. speed size etc).
3. Your code fails to allow passing a list of strings e.g.
"like","thus"
*Back to John*
I remember your line that moved me to action "I cant recall seeing a case
where I would consider optional parameters to be *better than overloading."
which suprisingly you still maintain.
Did you miss the "I cant recall bit.... *better than overloading"?When some posters make these throwaway comments, it puts novices on the
wrong track i.e. a blind belief that something is thus because they read it
somewhere.
Maybe by your reading. Others, I cant answer for but this is _not_ whatThat is how bad coding starts.
For instance I dislike the switch statement for a few reasons, but I'd never
say 'I've never seen a time when switch was preferable to using If's and
better structured code' because that would suggest to others that switch
shouldn't ever be used (and is also not entirely accurate). -And be honest
John that is essentially what you've been communicating with regards to
optional parameters.
Why would I post something like this when I have never taken this stance?This is probably going to be my last post in this thread (I hope), but if
you still are not convinced then try posting to this ng something like
'Optional params are a waste of time.
Just to reiterate.I cant recall seeing a case where I
would consider optional parameters to be *better than overloading'.
Hopefully a few mvps will succeed in convincing you where I seem to have
failed.
Oddball said:The thing with optional parameters vs. overloading
is that neither side can win because both sides have
a valid argument.
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.