Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 声明一个常量数组
我需要一个类中的 const 字符串数组。就像是
public class some_class_t { public const string[] names = new string[4] { "alpha", "beta", "gamma", "delta" }; }
但是这段代码会导致错误:
引用类型“string[]”的常量“names”只能用 null 初始化。
我该怎么办?
将其声明为,readonly而不是const:
readonly
const
public readonly string[] names = { "alpha", "beta", "gamma", "delta" };