当传递给方法,以便为参数进行评估? [英] Are parameters evaluated in order when passed into a method?

查看:166
本文介绍了当传递给方法,以便为参数进行评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是为了进行评估时,他们传递给方法的参数?

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

有关Java的它总是如此,对于C实在不行,但对于C#的答案是什么?

For Java it's always true, for C it isn't, but what is the answer for 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++];  
}

这样的作品,但它只能碰运气或规则?

This sample works, but is it only luck or a rule?

推荐答案

是的,作为方法的参数传递表达式始终计算从左到右。

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

从C#4.0语言规范:

From the C# 4.0 Language Specification:

7.5.1.2参数列表的运行时评估

在函数成员调用(第7.5.4节)的运行时处理,表达式或参数列表的变量引用是为了评估, 从左至右 [...]

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, [...]

这篇关于当传递给方法,以便为参数进行评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆