a{get{returnnewList{&quo"/> a{get{returnnewList{&quo">

Home >Backend Development >C#.Net Tutorial >Declare const arrays in C#

Declare const arrays in C#

王林
王林forward
2023-09-03 23:37:021412browse

在 C# 中声明 const 数组

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete