.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

Optional Parameters

So you have optional parameters for functions/methods in Visual Basic .NET, but not in C#?
Fire Fighter
Sunday, January 29, 2006
Thats right. Although optional parameters are actually a first class runtime feature, C# does not support them. The reasons why essentially come down the the language designers preference which could be debated until the cows come home.

Now - the way it works in VB.NET is that when an optional parameter is specified the parameter in IL is dectorated with the [opt] attribute. This tells other compilers that this is an optional parameter. The default value for the optional parameter is in the method prolog there.

So a compiler that supports default values can read that data and allow the developer not to have to provide null or the default value.

In the case of C#, it can call APIs that use optional parameters but it always has to provide the parameter.

One of the issues with optional parameters is that the default is statically compiled into the callsite so if the default changes - the change does not get reflected in the client code unless you recompile.

That could be seen as good or bad depending on your point of view.

- Mitch
Mitch Denny Send private email
Sunday, January 29, 2006
So, if you have to use two default arguments in a C# method, then I'll have to rewrite basically the same piece of function thrice because I'll have to fake the optional params by three overloads?
Water Cooler v2
Sunday, January 29, 2006
Yep. Although the way to look at it is am I being a good .NET citizen, especially if you are producing a library to be used by others. In that case you would implement the overloads anyway since lots of languages don't support optional parameters.

There are some scenarios that optional parameters support which overloading doesn't but most of the time overloading works just fine in C#.

I guess we should just be happy with have a choice of _different_ languages.

- Mitch
Mitch Denny Send private email
Sunday, January 29, 2006
Anyhow, it doesn't mean you write the same function three times.  You write the function once (with two params), and you call it from the other two overloads (with zero or one params), passing in the default values.  I usually do it this way in VB .NET instead of using optional parameters.  E.g.:

Sub DoSomething()
    Me.DoSomething(0, String.Empty)
End Sub

Sub DoSomething(number As Int32)
    Me.DoSomething(number, String.Empty)
End Sub

Sub DoSomething(number As Int32, text As String)
    ' the actual function code is here
End Sub
Kyralessa Send private email
Sunday, January 29, 2006
That's what I meant. But why would you do that in VB.NET, Kyralessa?
Fire Fighter
Sunday, January 29, 2006
Because if I wrote a library in VB .NET and a C# person wanted to use it, he'd always have to pass in the parameters, even if they were the default values.  (I just did a small test to confirm this.)  C# doesn't let you "opt out" of the optional parameters.

I use overloads instead because (a) it might not be clear what the default parameters were supposed to be to the library user, and (b) even if the library user does know what the default values should be, it's sloppy to require them.  I've simply trained myself not to use Optional in VB .NET.
Kyralessa Send private email
Sunday, January 29, 2006
Many VB.NET people give the same advice about not using Optional, although it's hard to resist the temptation when you are quickly hacking in something like Optional bSilent As Boolean = False.
Larry Lard Send private email
Monday, January 30, 2006
You could do this using the params keyword

The optional parameters must appear after the required ones.

public void DoSomething(string requiredParam, int requiredParam2, params object[] optionalParamArray) {

// ... method logic


Developers can call your method like this:

DoSomething("abc",123, 100);


DoSomething("abc",123, 100, true, MyObject);

adsense wannabe Send private email
Friday, February 03, 2006
Abusing "params object[]" in that way is not a good idea because it completely kills IntelliSense amd compile-time type checking. You have to refer to the text comments to figure out just which objects are acceptable, in what order, and what they mean.
Chris Nahr Send private email
Saturday, February 04, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz