In C#, use readonly to declare a const array.
public static readonly string[] a = { "Car", "Motorbike", "Cab" };
In readonly, unlike const, you can also set the value at runtime.
An alternative way to implement the above is -
public ReadOnlyCollection<string> a { get { return new List<string> { "Car", "Motorbike", "Cab" }.AsReadOnly();}}
.NET Framework 4.5 brings us improvements -
public ReadOnlyCollection<string> a { get; } = new ReadOnlyCollection<string>( new string[] { "Car", "Motorbike", "Cab" } );
The above is the detailed content of Declare const arrays in C#. For more information, please follow other related articles on the PHP Chinese website!