相关疑难解决方法(0)

const和readonly有什么区别?

是什么区别const,并readonly和你使用一个比其他?

.net c# const constants readonly

1269
推荐指数
19
解决办法
39万
查看次数

除字符串之外的引用类型的const字段只能使用null Error初始化

我正在尝试创建一个2D数组来存储一些不会像这样改变的值.

const int[,] hiveIndices = new int[,] {
{200,362},{250,370},{213,410} , 
{400,330} , {380,282} , {437, 295} ,
{325, 405} , {379,413} ,{343,453} , 
{450,382},{510,395},{468,430} ,
{585,330} , {645,340} , {603,375}
};
Run Code Online (Sandbox Code Playgroud)

但是在编译时我得到了这个错误

hiveIndices is of type 'int[*,*]'. 
A const field of a reference type other than string can only be initialized with null.
Run Code Online (Sandbox Code Playgroud)

如果我改变const to static,它会编译.我不明白添加const量词应该如何引发这种行为.

.net c#

19
推荐指数
1
解决办法
2万
查看次数

标签 统计

.net ×2

c# ×2

const ×1

constants ×1

readonly ×1