Why must default method parameters be compile-time constants in C# [closed]

我是研究僧i 提交于 2019-12-05 04:11:44

Because the spec says so:

A fixed-parameter with a default-argument is known as an optional parameter, whereas a fixed-parameter without a default-argument is a required parameter. A required parameter may not appear after an optional parameter in a formal-parameter-list. A ref or out parameter cannot have a default-argument. The expression in a default-argument must be one of the following:

• a constant-expression

• an expression of the form new S() where S is a value type

• an expression of the form default(S) where S is a value type

As to why the language designers chose to do this, that we can only guess at. However, another piece of the spec hints at an answer:

When arguments are omitted from a function member with corresponding optional parameters, the default arguments of the function member declaration are implicitly passed. Because these are always constant, their evaluation will not impact the evaluation order of the remaining arguments.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!