c# - 优雅的静态只读字符串集合分配

标签 c# .net

我想创建一个静态 ReadOnlyCollection与字符串。

有什么办法可以让这个表达式更短或更优雅吗?

public static readonly ReadOnlyCollection<string> ErrorList = new ReadOnlyCollection<string>(
  new string[] {
    "string1",
    "string2",
    "string3",
  }
);

我多次使用它,但在不同的文件中。

最佳答案

另一种选择是使用 List<T>带有集合初始值设定项和 AsReadOnly 方法:

public static readonly ReadOnlyCollection<string> ErrorList = new List<String> {
    "string1",
    "string2",
    "string3",
  }.AsReadOnly();

关于c# - 优雅的静态只读字符串集合分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062007/

相关文章:

.net - 有没有办法在文本文件中自动缩进 VB.NET 代码

c# - 如何在ListMetrics函数中使用nextToken

c++ - CLR ICLRControl::GetCLRManager 失败并出现错误 HOST_E_INVALIDOPERATION

c# - 使用 DotNetOpenAuth 设置带有子域标识符的 OpenID 提供程序

javascript - asp.net - 客户端控制更改在服务器端看不到

Linux 下的 C#,WorkingDirectory 无法正常工作

c# - .net Core 2.1 应用程序中的 Windows 身份验证

c# - 如何为 Windows Phone 创建应用程序文件(例如 .apk)?

c# - 从数据表导出到 excel 时,如何阻止前导 0 被剥离?

c# - 从静态函数访问外部变量