参数传入方法时是否按顺序求值?
对于 Java 它总是正确的,对于 C 它不是,但是对于 C# 的答案是什么?
示例
string.Format("byte1={0} byte2={1} byte3={2}",
getNextByte(),
getNextByte(),
getNextByte());
int pos=0;
byte[] arr=new byte[] {1,2,3,4,5,6};
byte getNextByte()
{
return arr[pos++];
}
这个示例有效,但这只是运气还是规则?
最佳答案
是的,作为参数传递给方法的表达式总是从左到右求值。
来自 C# 4.0 语言规范:
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, [...]
关于c# - 传递给方法时参数是否按顺序求值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359996/