Const array of strings [duplicate]

爱⌒轻易说出口 提交于 2020-02-26 08:47:28

问题


Possible Duplicate:
Declare a Const Array

I need an array of const strings in the class. Something like

public class some_class_t
{
    public const string[] names = new string[4]
    {
        "alpha", "beta", "gamma", "delta"
    };
}

But this code causes an error:

A constant 'names' of reference type 'string[]' can only be initialized with null.

What should I do?


回答1:


Declare it as readonly instead of const:

public readonly string[] names = { "alpha", "beta", "gamma", "delta" };


来源:https://stackoverflow.com/questions/14063203/const-array-of-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!