在C#中,使用readonly来声明一个const数组。
'public static readonly string[] a = { "Car", "Motorbike", "Cab" };
在readonly中,与const不同的是,您也可以在运行时设置值。
实现上述内容的另一种替代方法是 −
'public ReadOnlyCollection<string> a { get { return new List<string> { "Car", "Motorbike", "Cab" }.AsReadOnly();}}
.NET Framework 4.5 给我们带来了改进 -
'public ReadOnlyCollection<string> a { get; } = new ReadOnlyCollection<string>(
new string[] { "Car", "Motorbike", "Cab" }
);