在 C# 中声明一个 const 数组
在 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 框架 4.5 对上述实现了改进 −
public ReadOnlyCollection<string> a { get; } = new ReadOnlyCollection<string>(
new string[] { "Car", "Motorbike", "Cab" }
);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP