ここでは混乱があります。代入演算子のLHSのが最初に評価されます。特に、式df
と0
はGetLearningIndex
前が評価されるが、(インデックスの検証を含む)配列要素の割り当ては、結果が計算された後に起こります。
はここにいくつかの詳細を示した例です。この形の文でそう
using System;
public class Test
{
private int[] array = new int[10];
static void Main()
{
Test instance = null;
// This would throw a NullReferenceException
// because instance is null at the start of the statement.
// ExecuteSideEffect never gets called.
// instance.array[100] = ExecuteSideEffect(() => instance = new Test());
instance = new Test();
// This would throw an IndexOutOfBoundsException
// because instance.array is evaluated before ExecuteSideEffect.
// The exception is only thrown when the assignment is performed.
// instance.array[100] = ExecuteSideEffect(() => instance.array = new int[1000]);
int index = 5;
// This modifies array index 5 because index is evaluated
// before EvaluateSideEffect
instance.array[index] = ExecuteSideEffect(() => index = 1);
Console.WriteLine(instance.array[5]); // 10
}
private static int ExecuteSideEffect(Action action)
{
action();
return 10;
}
}
:
arrayExpression[indexExpression] = valueExpression;
実行順序は次のとおりです。
- は
arrayExpression
を評価します。結果がnullではないというチェックはありませんが、式の評価自体がNullReferenceException
です。
indexExpression
を評価してください。この時点で、アレイに対して境界チェックは実行されません。
- は
valueExpression
を評価する配列要素を配列参照がnullであり、配列インデックスが有効であることのチェックがされている3 これがあるステップの結果にステップ1及び2の結果を用いて表現設定実行されます。
これは現在のところ私が言うことができる限り厳密に指定されています。問題をECMA C#5標準で修正できるかどうかを確認します。
リンク先のドキュメントは、java用です。 – Stuart
あなたがリンクしているドキュメントは、その割り当てが右から左への結合であることを示しています。割り当てを評価する前に割り当てを試すことはできません。 –