A const field of a reference type other than string can only be initialized with null Error [duplicate]
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]的更多相关文章
- 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 ...
- C#中的值类型(value type)与引用类型(reference type)的区别
ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部 C#中 ...
- 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '
没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...
- Reference Type Casting
5.5.1. Reference Type Casting Given a compile-time reference type S (source) and a compile-time refe ...
- 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 ...
- mybatis No enum const class org.apache.ibatis.type.JdbcType.Date 坑爹的配置
转自:https://lihaiming.iteye.com/blog/2248059 在ibatis中不需要关注这些参数 而转到mybatis后 如果字段值为空 必须设置jdbcType如inser ...
- error: field 'b' has imcomplete type
在下面的程序中,在编译时会遇到下面的错误: error: field 'b' has incomplete type 域b是一个不完备的类型,即class B的声明不完备 #include <i ...
- 引用类型 (Reference Type Matters)、扩展与派发方式
引用类型 (Reference Type Matters) 引用的类型决定了派发的方式. 这很显而易见, 但也是决定性的差异. 一个比较常见的疑惑, 发生在一个协议拓展和类型拓展同时实现了同一个函数的 ...
- 快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性
Visual C# 8.0中引入了可空引用类型(Nullable reference type),通过编译器提供的强大功能,帮助开发人员尽可能地规避由空引用带来的代码问题.这里我大致介绍一下可空引用类 ...
随机推荐
- trigger()的event事件对象之坑
问题引入,先贴一段有问题的代码,如果你对 trigger() 这个函数了解不透彻,还真看不出这段代码错在哪.完成的功能是样式转换器,想让页面在加载后自行触发点击事件隐藏三个按钮,但是效果如图并没有隐 ...
- oracle 数据库导入导出
要把公司的数据库导入到自己的电脑上(都需要再命令窗口下输入指令) 导出数据库的基本代码: exp zj_user_kf/oracle@tzsw_4 file=d:\test3.dmp full=y 导 ...
- 洛谷 P1886 滑动窗口
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- TCP通信三次握手的过程
过程 编辑 第一次 第一次握手:建立连接时,客户端 发送syn包(syn=j)到服务器,并进入SYN_SENT状态,等待服务器确认:SYN:同步序列编号(Synchronize Sequence Nu ...
- debian终端菱形乱码修复
最简安装debian的时候由于没有中文字库,若选择看中文环境会出现菱形乱码.先把zh.utf8换为us.utf8看着好顺眼些.按空格键取消已选的zh.utf8选项按空格键选择us.utf8选项ok
- PHP 正则表达式替换一部分内容
preg_replace('/&topic=(.*?)&type=/',"&topic={$data['topic']}&type=",$postF ...
- Nginx的平滑重启和平滑升级
一,Nginx的平滑重启如果改变了Nginx的配置文件(nginx.conf),想重启Nginx,可以发送系统信号给Nginx主进程的方式来进行.在重启之前,要确认Nginx配置文件的语法是正确的. ...
- 单选复选按钮以及Toast学习笔记
1:单选按钮是以组的形式呈现,xml布局文件中需要定义一个RadioGroup,然后在这个组内再定义RadioButton.在java代码中为该按钮添加监听时,需要用组名来引用对应的方法setOnCh ...
- Extjs combox的详解
Extjs combox的详解 写了哈extjs当中的combox,第一次写,照着网上的例子抄.在上次的例子中,是实现了,可是有一个重大的错误.也就是自己根本没有理解combox从远程服务器获取数据, ...
- jQuery实现分页
转载地址 http://www.cnblogs.com/xiaoruoen/archive/2012/01/11/2318199.html ;( function($){ $.extend({ &qu ...