Optional Parameters

  • Thread starter Thread starter Water Cooler v2
  • Start date Start date
W

Water Cooler v2

So you have optional parameters for functions/methods in Visual Basic
..NET, but not in C#?
 
correct - optional parameters are not part of the C# language specification,
however the same effect can be achieved by Method overloading for most cases.
 
Note that you can create methods with optional parameters in C# using
OptionalAttribute, you just can't consume them from C# (though you can from
VB) and I don't think there's any way of specifying the default value for
the optional argument which is a major limitation anyway - you will just get
the default value for the type I think.
 
you can use variable number of parameters in C#, I guess with the params[]
keyword.... or object[]....
 
Mark said:
correct - optional parameters are not part of the C# language specification,
however the same effect can be achieved by Method overloading for most cases.

Mark is right. Optional parameters are a Visual Basic *shortcut* to
method overloading. That's why I tend to avoid them. Plus, in my eyes,
they just look clunky and are harder to maintain.
 
Clive Dixon said:
Note that you can create methods with optional parameters in C# using
OptionalAttribute, you just can't consume them from C# (though you can
from VB) and I don't think there's any way of specifying the default value
for the optional argument which is a major limitation anyway - you will
just get the default value for the type I think.

FYI

There is a well known problem with optional parameters and versioning which
essentially boils down to the same thing as the difference between

public const int x = 3
and
public static readonly int x = 3

One is compile time and one is runtime.
This can cause a lot of confusion if you send out supposedly compatible dll
fix with different optional parameters.
 
Back
Top