問題描述
增加和減少混淆 (Incrementing and Decrementing Confusion)
我對以下行為有點困惑:
int a = 3;
a++;
in b = a;
我知道當您執行 a++
時,它會添加一個 1,從而使 a = 4 現在
b
等於 a
所以它們都是 4。
int c = 3;
int d = c;
c++
但是,這裡它告訴我 c
是 4 而 d
是 3。因為 c++
使得 c = 4
; d = 4;
也不會嗎?
參考解法
方法 1:
This line:
int d = c;
says "Declare a variable called d
, of type int
, and make its initial value equal to the current value of d
."
It doesn't declare a permanent connection between d
and c
. It just uses the current value of c
for the initial value of d
. Assignment works the same way:
int a = 10;
int b = 20; // Irrelevant, really...
b = a; // This just copies the current value of a (10) into b
a++;
Console.WriteLine(b); // Still 10...
參考文件