2011-04-28 34 views
1

我們能在C#使用檢查檢查溢出爲比特的特定數目,如說25,30等檢查關鍵字用於檢查在c#指定的位數

int A = 0; 
int B = 1000; 

checked 
{ 
    A += 1000000; 
    B = B * A; 
} 

例如,在上述例如,A可以檢查27位溢出。

+0

請提供一些關於您的問題的信息。你的問題似乎是比原來的問題更多的解決方案。 – PVitt 2011-04-28 08:42:27

回答

1

不,沒有像C#支持的東西。

你也許能來是寫自己的方法,該方法用於檢查「正常」類型的溢出(爲int 32位,64 long等),然後強加於有效一些額外的限制,最近值。理想情況下,我建議爲此創建自己的包裝類型,例如

public struct Int25 
{ 
    private readonly int value; 

    // Constructor etc, and operators which always make sure the result 
    // is within the appropriate range 
}