常量字符串数组 [英] Const array of strings
本文介绍了常量字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
声明一个常量数组
Possible Duplicate:
Declare a Const Array
我在类中需要一个const字符串数组.像
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:
引用类型为'string []'的常量'names'只能使用null初始化.
A constant 'names' of reference type 'string[]' can only be initialized with null.
我该怎么办?
推荐答案
将其声明为readonly
而不是const
:
public readonly string[] names = { "alpha", "beta", "gamma", "delta" };
这篇关于常量字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文