我有點困惑的以下行爲:遞增和遞減混亂
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;
也不會?
我有點困惑的以下行爲:遞增和遞減混亂
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;
也不會?
這條線:
int d = c;
說「聲明一個稱爲d
變量,int
型的,並且使其初始值等於d
當前值。」
它沒有宣佈d
和c
之間的永久連接。它只是使用當前值c
初始值爲d
。分配的工作方式相同:?C#引用賦值運算符]
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...
主要是因爲int是值類型不是引用類型 – TheLethalCoder
@TheLethalCoder:即使使用引用類型,您也只能將值從一個變量複製到另一個變量中......更改一個變量的值不會改變另一個變量的值。修改對象這兩個變量的值引用是另一回事。 –
的可能的複製(http://stackoverflow.com/questions/7844791/c-sharp-reference-assignment-operator) –