2011-09-22 50 views
3

Possible Duplicate:
C# int, Int32 and enum's更改基礎類型的枚舉的長

C#允許您爲基礎類型的枚舉設置爲長。但是,你會如何解釋行爲的差異,當你嘗試編譯如下兩條語句:

public enum Colors : long 
{ 
    Blue = 512L, 
    Purple = 1024L 
} 

public enum Colors : System.Int64 
{ 
    Blue = 512L, 
    Purple = 1024L 
} 

第一個編譯OK(附:長),而第二(與:System.Int64)不會編譯 - 你得到一個錯誤:類型字節,sbyte,短,ushort,int,uint,long或ulong預計注意:顯然,我明白錯誤消息。我感到困惑的是,我認爲「長」或多或少是「Int64」的別名

+0

嗯...... @BoltClock。你的鏈接很有用。它對這個問題有一個健康的討論。然而,很難推斷它僅通過查看其標題來解決相同的主題。無論如何,謝謝。 –

回答

2

它是C#編譯器的限制,並且不支持Enum。您將不得不使用long亞歷克斯·特納(Visual Basic和C#項目經理)在MSFT:

Posted by Microsoft on 6/25/2010 at 8:53 AM Thanks for the suggestion for Visual Studio!

As you point out, we could enable support for saying Int16 instead of int here, but this would not provide any extra expressiveness to C# programs (and it's more characters to type!). We'd be unlikely to invest our resources to add this support to Enums.

+0

它實際上現在已經修復,請參閱https://stackoverflow.com/questions/37589056/why-is-this-enum-declaration-working-now – Mafii