我想创建一个静态 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/