卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

在 C# 中声明 const 数组

在 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 Framework 4.5 给我们带来了改进 -

'
public ReadOnlyCollection<string> a { get; } = new ReadOnlyCollection<string>(
new string[] { "Car", "Motorbike", "Cab" }
);
卓越飞翔博客
上一篇: 创建您的初创公司:使用 Ajax 简化会议安排
下一篇: C# 中的后台工作者类
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏