Are parameters evaluated in order when passed into a method?

后端 未结 3 1420
春和景丽
春和景丽 2020-12-11 00:03

Are parameters evaluated in order when they passed into a method?

For Java it\'s always true, for C it isn\'t, but what is the answer for C#?

Sample

相关标签:
3条回答
  • 2020-12-11 00:43

    As others have pointed out, the language specification requires that parameters be evaluated in left-to-right order.

    However, full disclosure, we accidentally and not on purpose introduced a couple of bugs in C# 4.0 where certain scenarios involving named arguments, optional parameters and ref-omitted parameters in calls to legacy COM objects, such that in those scenarios the side effects of arguments might not be evaluated in strictly left-to-right order. The analyzer that deals with the interactions between those features is complicated and it had some bugs.

    I apologize for the errors; we hope to have them fixed in the next version.

    0 讨论(0)
  • 2020-12-11 00:47

    Yes, expressions passed as arguments to methods are always evaluated from left to right.

    From the C# 4.0 Language Specification:

    7.5.1.2 Run-time evaluation of argument lists

    During the run-time processing of a function member invocation (§7.5.4), the expressions or variable references of an argument list are evaluated in order, from left to right, [...]

    0 讨论(0)
  • 2020-12-11 00:52

    From the language specification:

    During the run-time processing of a function member invocation, the expressions or variable references of an argument list are evaluated in order, from left to right.

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