我想要一个私有(private)的常量成员
private static readonly HashSet<char> _nameChars
在我的 Controller 中,包含字符 ' '、'a'、'A'、...、'z'、'Z'
。目的是用于表单的服务器端验证。但是如何初始化 HashSet
?
private static readonly HashSet<char> _nameChars = ?????
有什么好的方法吗?我知道 C++ 对 std::set
进行了括号初始化。
最佳答案
C# 也有集合初始化器:
private static readonly HashSet<char> _nameChars = new HashSet<char> { 'a', 'b', ...};
你也可以使用一个接受 IEnumerable<T>
的构造函数参数:
private static readonly HashSet<char> _nameChars = new HashSet<char>("abcde...");
我发现第二种方法更具可读性。
关于c# - 如何初始化 private static readonly HashSet<char>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33547420/