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

Declare const arrays in C#

王林
Release: 2023-09-03 23:37:02
forward
1419 people have browsed it

在 C# 中声明 const 数组

In C#, use readonly to declare a const array.

public static readonly string[] a = { "Car", "Motorbike", "Cab" };
Copy after login

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();}}
Copy after login

.NET Framework 4.5 brings us improvements -

public ReadOnlyCollection<string> a { get; } = new ReadOnlyCollection<string>(
new string[] { "Car", "Motorbike", "Cab" }
);
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template