OptionalAttribute parameter's default value?

前端 未结 1 1604
暗喜
暗喜 2021-02-19 01:09

MSDN\'s VS2010 Named and Optional Arguments (C# Programming Guide) tells us about optional parameters in C#, showing code like I\'d expect:

public void ExampleMe         


        
相关标签:
1条回答
  • 2021-02-19 01:27

    The rules are this:

    • For parameters of type object, Type.Missing is passed.
    • For other reference types, null is passed.
    • For value types, the default of the value type is passed.
      • For Nullable<T> this means that you will get a Nullable<T> instance which is equal to null (the HasValue property will return false)

    Note that in the case of everything except parameters of type object, it's the equivalent of default(T).

    I was a little surprised, as the C# 4.0 specification didn't indicate what the outcome would be, and I'd expect it to be there.

    Also (as indicated by Scott Rippey in the comments), this is evaluated at compile-time, this is not a run-time operation, meaning that if you have calls to this method in other assemblies which are already deployed, and you change the optional value, the default passed to the method will not change unless you compile everything that makes the call against the method in the assembly.

    0 讨论(0)
提交回复
热议问题