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
*/

  

A const field of a reference type other than string can only be initialized with null Error [duplicate]的更多相关文章

  1. passing argument 3 of ‘wtk_hlv_rec_init’ discards ‘const’ qualifier from pointer target type

    -Werror,编译出现如下错误: src/wtk/exam/wtk_ndx.c:154:6: error: passing argument 3 of ‘wtk_hlv_rec_init’ disc ...

  2. C#中的值类型(value type)与引用类型(reference type)的区别

    ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部     C#中 ...

  3. 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '

    没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...

  4. Reference Type Casting

    5.5.1. Reference Type Casting Given a compile-time reference type S (source) and a compile-time refe ...

  5. java 反射 报错:Attempt to get java.lang.Integer field "..." with illegal data type conversion to int

    类: Integer id; 反射时: Field f = User.class.getDeclaredField("id"); f.setAccessible(true); in ...

  6. mybatis No enum const class org.apache.ibatis.type.JdbcType.Date 坑爹的配置

    转自:https://lihaiming.iteye.com/blog/2248059 在ibatis中不需要关注这些参数 而转到mybatis后 如果字段值为空 必须设置jdbcType如inser ...

  7. error: field 'b' has imcomplete type

    在下面的程序中,在编译时会遇到下面的错误: error: field 'b' has incomplete type 域b是一个不完备的类型,即class B的声明不完备 #include <i ...

  8. 引用类型 (Reference Type Matters)、扩展与派发方式

    引用类型 (Reference Type Matters) 引用的类型决定了派发的方式. 这很显而易见, 但也是决定性的差异. 一个比较常见的疑惑, 发生在一个协议拓展和类型拓展同时实现了同一个函数的 ...

  9. 快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性

    Visual C# 8.0中引入了可空引用类型(Nullable reference type),通过编译器提供的强大功能,帮助开发人员尽可能地规避由空引用带来的代码问题.这里我大致介绍一下可空引用类 ...

随机推荐

  1. AMQ学习笔记 - 19. 问题解决 - 控制Atomikos的日志输出

    概述 在使用Atomikos为ActiveMQ提供JTA支持时,Atomikos在控制台打印了繁琐的日志.这里介绍如何控制Atomikos日志输出的粒度. 解决方案 基于以下三个事实: Atomiko ...

  2. 4月13日学习笔记——jQuery工具函数

    浏览器及特性检测 jQuery.support.boxModel 如果这个页面和浏览器是以 W3C CSS 盒式模型来渲染的,则等于 true.通常在 IE 6 和 IE 7 的怪癖模式中这个值是 f ...

  3. Windows下安装GnuRadio最简单的方法(没有之一)

    作者在Windows XP SP3 32位下亲测通过,理论上Win7也没问题. 1. 如果系统中安装有Python,请先把Python卸载. 2. 下载安装Python(x,y) 2.7.5.0, 下 ...

  4. java.lang.ThreadLocal源码分析

    ThreadLocal类提供线程本地变量,为变量在每个线程创建一个副本,每个线程可以访问自己内部的副本变量. 比如,有这样一个需求,需要为每个线程创建一个独一无二的标识,这个标识在第一次调用Threa ...

  5. U-Boot--配置过程分析

    arm核:arm920t    2410 uboot版本:u-boot1.1.6 通过readme可以知道配置执行make smdk2410_config命令. 顶层Makefile : smdk24 ...

  6. [大牛翻译系列]Hadoop(12)MapReduce 性能调优:诊断硬件性能瓶颈

    6.2.5 硬件性能问题 尽管单独的硬件的MTTF(平均失效前时间)都数以年记,然而在集群中就完全不是这么一回事了.整个集群的MTTF就要小得多.这一节要介绍如何确定CPU,内存,磁盘和网络是否过度利 ...

  7. sqlServer2008 手工注入

    接着上一篇的<mysql手工注入> 参考:http://hi.baidu.com/ciqing_s/item/971bf994365130accc80e5ed http://hi.baid ...

  8. ASP.NET Core 行军记 -----拔营启程

    ASP.NET MVC 6:https://docs.asp.net/en/latest/mvc/index.html ASP.NET Core :https://docs.asp.net/en/la ...

  9. hdu 5451 Best Solver 矩阵循环群+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x    求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...

  10. EditPlus64的安装配置

    下载地址,直接到360下载即可,下载完毕之后,进入如下网址,完后在线生成注册码 http://www.jb51.net/tools/editplus/ 以上是文本编辑器EditPlus的安装以及注册, ...