A const field of a reference type other than string can only be initialized with null Error [duplica...

C#常量2D数组初始化问题
本文探讨了在C#中尝试将一个二维整数数组声明为const类型时遇到的问题。由于const类型的字段不能使用new操作符来初始化引用类型的实例,因此这种初始化方式会导致编译错误。文章给出了正确的做法,并解释了如何使用readonly属性来替代。

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

  

 

转载于:https://www.cnblogs.com/chongxin/p/4170829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值