隱含爲static
必須在聲明時初始化
必須被初始化爲編譯時常量值
只有簡單類型,枚舉,字符串纔可以是常量
常量字段代碼:
class Pair
{
public Pair(int x, int y)
{
// ???
}
...
private const int x = 0, y = 0;
}
代碼解析:
在C#中,常量字段隱含爲static,但你不能顯式聲明一個常量字段是static:
static const int x = 0;//錯誤
常量必須被初始化,並且只能在聲明時初始化:
const int x;//錯誤
常量必須被初始化爲編譯時常量值:
const int x = Method();//錯誤
只有簡單類型,枚舉,字符串才能被聲明爲常量:
const Pair p = new Pair();//錯誤