views:

296

answers:

2

Does C# 4.0 allow optional out or ref arguments?

+1  A: 

Optional parameters can't have ref or out modifiers.

Jason Rowe
Reference, please.
Robert Harvey
C# in depth 2nd Edition by Jon Skeet.
Jason Rowe
Makes sense, when you think about it.
Robert Harvey
@Robert I disagree that it makes sense.
Chris Marisic
+3  A: 

As already mentioned, this is simply not allowed and I think it makes a very good sense. However, to add some more details, here is a quote from the C# 4.0 Specification, section 21.1:

Formal parameters of constructors, methods, indexers and delegate types can be declared optional:

fixed-parameter:
    attributesopt parameter-modifieropt type identifier default-argumentopt
default-argument:
    = expression

  • A fixed-parameter with a default-argument is an optional parameter, whereas a fixed-parameter without a default-argument is a required parameter.
  • A required parameter cannot appear after an optional parameter in a formal-parameter-list.
  • A ref or out parameter cannot have a default-argument.
Tomas Petricek