11

Also is there a way to use run-time values for optional method parameters?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • possible duplicate of [Cannot use String.Empty as a default value for an optional parameter in C# - then what's the point?](http://stackoverflow.com/questions/2701314/cannot-use-string-empty-as-a-default-value-for-an-optional-parameter-in-c-sharp) – nawfal May 16 '13 at 11:01

3 Answers3

12

Optional parameters are required to be constants because they are written out as values of an attribute. Hence they inherit all of the restrictions that an attribute value has.

There is no way to directly encode a runtime value. However you can get close with the following pattern

public void MyApi(SomeType type = null) {
  type = type ?? new SomeType();
  ...
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    Thanks, that seems like a neat trick. Out of curiosity, do you know why they are implemented using attributes? I assume to reuse what's already in the framework? – Joan Venge Mar 18 '11 at 00:15
  • 1
    @Joan IIRC it is done with attributes because that's how other languages (VB.Net) do it and hence it provides compatibility with them. – JaredPar Mar 18 '11 at 00:16
  • 1
    @Joan: By using attributes, they don't break languages that don't have this feature. Remember, everything in .NET gets compiled into a form that needs to be language neutral... Attributes are a clean way to handle optional parameters in a specific language, without requiring breaking changes to other .NET languages. – Reed Copsey Mar 18 '11 at 00:16
  • @Jared: Thanks, but now I realize, if the trick you showed wouldn't work for value types, or reference types where you legitimately want to pass null, right? – Joan Venge Mar 18 '11 at 00:20
  • @Reed: Thanks that's a good point. But if it was an extended feature of IL, would it still break it for other languages? Like say something extra that only C# outputs to IL? – Joan Venge Mar 18 '11 at 00:21
  • 2
    @Joan: That would require a CLR change just to support this feature - the way they did it, it matched the existing VB implementation, has no impact on other languages, and "just works"... – Reed Copsey Mar 18 '11 at 00:51
  • 1
    Yes that's what I meant. That makes sense. They just went with the easiest route then. – Joan Venge Mar 18 '11 at 01:22
5

Optional parameters are compiled into the assembly and as such (just like anything that is designated as const) they must be a compile-time constant.

And no, you cannot use execution-time values as optional parameters.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    Thanks but can't it compile with a reference to a run-time reference? So say something like: this.Filename, etc within the same class? – Joan Venge Mar 18 '11 at 00:13
5

Optional parameters are determined at compile time, and substituted into the method if you call a method with too few parameters. They are handled via adding an attribute to the parameter in the method's IL.

As such, they need to be fully resolved at compile time (both for creation, since they're an attribute, but also when used). There is no way to use runtime values for optional method parameters.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373