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