According to the documentation?: (Ternary operator) is right associative, i.e. is it done like this a? b: (c? d: e)
https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/operators/conditional-operator
Why does this code show the operation of a ternary operator as a left associative? Those. the call to the property Cnt should have happened exactly 2 times (with x == 3), and as a result we get only one call.
static class Counter { public static int cnt; public static int Cnt { get { cnt++; return cnt; } } } public static void Main() { int someValue = 128; int x = int.Parse(Console.ReadLine()); var tmp = (x == 3) ? Counter.Cnt : (x == 3) ? Counter.Cnt : someValue; // Выполнение справа-налево Console.WriteLine(tmp + " cnt = " + Counter.cnt); // Обратите внимание, что здесь обращение напрямую к полю класса }
Cntin the response branch whileCntcondition either [1] or [2], respectively, the call occurs once. - nick_n_a