在 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