I know the below code will lead to undefined behaviour according to c/c++ standard but what about in c#? ,After some searching I found that in c# all the arguments/variables in an expression are evaluated from left to right(please correct me if am wrong), If this is true than the result(output of res variable) of below program should be 3, but its 4 ??.
class Program
{
static void Main(string[] args)
{
int a = 1;
int res = (a++) + (++a); //Will This Lead to Undefined Behavior(Like in C/C++)..??
Console.WriteLine(res);
Console.ReadLine();
}
}
The result is fine for these expressions when checked with left to right evaluation.
res = a + ++a; \\Successfully evaluates res to 3
res = ++a + a; \\Sussessfully evaluates res to 4
res = ++a + a++; \\Successfully evaluates res to 4
Similarly
res= a++ + ++a ;\\should be 3, why i get it 4 ??
Can Anybody explain am confused.??