Why do optional parameters in C# 4.0 require compile-time constants?

前端 未结 3 1578
野性不改
野性不改 2021-02-08 04:34

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

3条回答
  •  庸人自扰
    2021-02-08 04:50

    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.

提交回复
热议问题