»
S
I
D
E
B
A
R
«
Short, Int, 加加減減
February 16th, 2006 by kanru

short x = 32767;
x = x + 2;

在 C 裡面,我們可能不小心寫出這樣的 code 而不自覺會造成 overflow,但是在 C# 則會得到

error CS0266: Cannot implicitly convert type ‘int’ to ‘short’. An explicit conversion exists (are you missing a cast?)

要改成

x = (short)(x +2);

才行; 不過神奇的是,如果寫成

x += 2;

則不會有任何問題的直接 overflow… 這究竟是什麼樣的設計考量呢?


One Response  
  • neo anderson writes:
    April 4th, 2006 at 9:37 pm

    那應該是 implicitly casting


»  Substance: WordPress   »  Style: Ahren Ahimsa
© Copyright 2004-2009 Kan-Ru Chen