i++和++i的区别

先看如下程序:

class Program
    {
        static void Main(string[] args)
        {
            int x = 0;
            int y = 0;
            int i = 2;
            int j = 3;
            x = i++;
            Console.WriteLine("x={0},i={1}",x,i);

            y = ++j;
            Console.WriteLine("y={0},j={1}", y, j);

            Console.ReadKey();
        }
    }

输出结果如下:

x=2,i=3

y=4,j=4   如下图:

可以得出结论:

x = i++;等价于 x=i; i=i+1; 即先赋值,然后再自增

y =++j;等价于 j=j+1;y=j;  即先自增,然后再赋值

原文地址:https://www.cnblogs.com/qk2014/p/4389351.html