C# 字符串常量数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14063203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:34:41  来源:igfitidea点击:

Const array of strings

c#.netarraysstringconst

提问by Newbee

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.

引用类型 'string[]' 的常量 'names' 只能用 null 初始化。

What should I do?

我该怎么办?

采纳答案by John Woo

Declare it as readonlyinstead of const:

将其声明为readonly而不是const

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