I'm trying to create a 2D array to store some values that don't change like this.

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}
};

But while compiling I get this error

hiveIndices is of type 'int[*,*]'.
A const field of a reference type other than string can only be initialized with null.

1 Answer

Actually you are trying to make the array - which is a reference type - const - this would not affect mutability of its values at all (you still can mutate any value within the array) - making the array readonly would make it compile, but not have the desired effect either. Constant expressions have to be fully evaluated at compile time, hence the new operator is not allowed.

You might be looking for ReadOnlyCollection<T>

For more see the corresponding Compiler Error CS0134:

  http://msdn.microsoft.com/en-us/library/ms228606.aspx

'variable' is of type 'type'. A const field of a reference type other than string can only be initialized with null.
A constant-expression is an expression that can be fully evaluated at compile-time.
    Because the only way to create a non-null value of a reference-type is to apply the new operator, and because the new operator is not permitted in a constant-expression,
the only possible value for constants of reference-types other than string is null.
If you encounter this error by trying to create a const string array, the solution is to make the array readonly, and initialize it in the
constructor.

C#: Static readonly vs const ?

http://stackoverflow.com/questions/755685/c-static-readonly-vs-const

The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field.
A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.
Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:

http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.100%29.aspx

 public class ReadOnlyTest
{
class SampleClass
{
public int x;
// Initialize a readonly field
public readonly int y = 25;
public readonly int z; public SampleClass()
{
// Initialize a readonly instance field
z = 24;
} public SampleClass(int p1, int p2, int p3)
{
x = p1;
y = p2;
z = p3;
}
} static void Main()
{
SampleClass p1 = new SampleClass(11, 21, 32); // OK
Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z);
SampleClass p2 = new SampleClass();
p2.x = 55; // OK
Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z);
}
}
/*
Output:
p1: x=11, y=21, z=32
p2: x=55, y=25, z=24
*/

  

最新文章

  1. CCPC2016沈阳站
  2. P3369 【模板】普通平衡树(Treap/SBT)
  3. Apache commons-codec笔记
  4. CSS线性渐变
  5. AES加密 16进制与二进制转换
  6. Twenty Questions
  7. Linux学习之路:变量值的删除、替代和替换
  8. WPF WebBrowser 不可见问题的解析[转]
  9. 登录网站爬虫(保持Cookie不变)
  10. django的models模型类的常用数据类型和选项
  11. 分库分表、读写分离——用Sql和ORM(EF)来实现
  12. spring整合mybatisXML版
  13. 【做题】cf603E——线段树分治
  14. 损失函数Center Loss 代码解析
  15. 关于Virtual Box虚拟机里的系统不能启动的解决方法
  16. 不重启修改&#39;log_slave_updates&#39;变量
  17. CF1042C Array Product 分类讨论+贪心
  18. PHP设计超级好用的文件上传处理类一 (37)
  19. 创建第一个flask项目
  20. Hibernate(六)

热门文章

  1. 10 个超酷的 HTML5/CSS3 应用及源码
  2. Shell指令
  3. springmvc 精华
  4. Myeclipse安装svn插件(link方式)
  5. change
  6. ServletContext的用途
  7. mysql数据库创建database(实例),和用户,并授权
  8. 《CDN web加速代理》RHEL6
  9. PCB常用度量衡单位
  10. 使用HttpWebRequest以及HttpWebResponse读取Http远程文件