我有一个通用词典词典,我想基本上做一个克隆()的任何建议.
核心C#库中是否有任何可以为我提供不可变字典的东西?
Java的一些东西:
Collections.unmodifiableMap(myMap);
Run Code Online (Sandbox Code Playgroud)
而且只是为了澄清,我不打算阻止键/值本身被改变,只是字典的结构.如果任何IDictionary的mutator方法被调用(Add, Remove, Clear),我想要快速和大声失败的东西.
我在公共静态类中有以下代码:
public static class MyList
{
public static readonly SortedList<int, List<myObj>> CharList;
// ...etc.
}
Run Code Online (Sandbox Code Playgroud)
..但即使使用readonly我仍然可以从另一个类添加项目到列表:
MyList.CharList[100] = new List<myObj>() { new myObj(30, 30) };
Run Code Online (Sandbox Code Playgroud)
要么
MyList.CharList.Add(new List<myObj>() { new myObj(30, 30) });
Run Code Online (Sandbox Code Playgroud)
有没有办法让事物只读而不改变CharList的实现(它会打破一些东西)?如果我必须改变实现(使其不可更改),那么最好的方法是什么?我需要它是List <T,T>,所以ReadOnlyCollection不会这样做
我有一个Dictionary<string, List<string>>并希望将该成员公开为只读.我看到我可以将它作为一个返回IReadOnlyDictionary<string, List<string>>,但我无法弄清楚如何将它作为一个返回IReadOnlyDictionary<string, IReadOnlyList<string>>.
有没有办法做到这一点?在c ++中,我只是使用const,但C#没有.
请注意,IReadOnlyDictionary在这种情况下简单地使用a 没有帮助,因为我希望只读取值.看来唯一的方法是构建另一个IReadOnlyDictionary,并向它们添加IReadOnlyList.
另一个我不会兴奋的选择是创建实现接口IReadOnlyDictionary>的包装器,并让它保存原始实例的副本,但这似乎有点过分.
有没有办法在c#中定义枚举如下?
public enum MyEnum : string
{
EnGb = "en-gb",
FaIr = "fa-ir",
...
}
Run Code Online (Sandbox Code Playgroud)
好的,根据erick方法和链接,我用它来检查提供的描述中的有效值:
public static bool IsValidDescription(string description)
{
var enumType = typeof(Culture);
foreach (Enum val in Enum.GetValues(enumType))
{
FieldInfo fi = enumType.GetField(val.ToString());
AmbientValueAttribute[] attributes = (AmbientValueAttribute[])fi.GetCustomAttributes(typeof(AmbientValueAttribute), false);
AmbientValueAttribute attr = attributes[0];
if (attr.Value.ToString() == description)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
有什么改进?
在C#中是否有任何已建立的返回只读二维数组的方法?
我知道ReadOnlyCollection对于一维数组使用是正确的,我很乐意编写自己的包装类来实现一个this[] {get}.但是如果这个轮子已经存在,我不想重新发明轮子.
来自msdn:
表示键/值对的通用只读集合.
但请考虑以下事项
class Test
{
public IReadOnlyDictionary<string, string> Dictionary { get; } = new Dictionary<string, string>
{
{ "1", "111" },
{ "2", "222" },
{ "3", "333" },
};
public IReadOnlyList<string> List { get; } =
(new List<string> { "1", "2", "3" }).AsReadOnly();
}
class Program
{
static void Main(string[] args)
{
var test = new Test();
var dictionary = (Dictionary<string, string>)test.Dictionary; // possible
dictionary.Add("4", "444"); // possible
dictionary.Remove("3"); // possible
var list = (List<string>)test.List; // …Run Code Online (Sandbox Code Playgroud) 我有一个C#regex-parser程序,里面有三个文件,每个文件都包含一个静态类:
1)一个填充字符串字典的静态类
static class MyStringDicts
{
internal static readonly Dictionary<string, string> USstates =
new Dictionary<string, string>()
{
{ "ALABAMA", "AL" },
{ "ALASKA", "AK" },
{ "AMERICAN SAMOA", "AS" },
{ "ARIZONA", "AZ" },
{ "ARKANSAS", "AR" }
// and so on
}
// and some other dictionaries
}
Run Code Online (Sandbox Code Playgroud)
2)将这些值编译为正则表达式的类
public static class Patterns
{
Public static readonly string StateUS =
@"\b(?<STATE>" + CharTree.GenerateRegex(Enumerable.Union(
AddrVals.USstates.Keys,
AddrVals.USstates.Values))
+ @")\b";
//and some more like these
}
Run Code Online (Sandbox Code Playgroud)
3)一些基于这些字符串运行正则表达式的代码:
public static class Parser
{ …Run Code Online (Sandbox Code Playgroud) 可能重复:
.NET中是否有只读的通用字典?
嗨,大家好,
我意识到可以将列表作为只读集合返回:
var list = new List<string>();
return list.AsReadOnly();
Run Code Online (Sandbox Code Playgroud)
我想为像Dictionary或HashSet这样的数据结构编写类似的代码,但我没有找到实现这一目标的方法.有没有办法将Dictionary或HashSet作为只读集合返回?
谢谢.
我想知道我是否可以创建一个返回只读字典的属性?
例:
Run Code Online (Sandbox Code Playgroud)private readonly Dictionary<string, IMyObject> _myDictionary; public Dictionary<string, IMyObject> MyDictionary { get { return _myDictionary; } }
因此,不允许使用MyDictionary的人添加,删除或更改项目.有什么方法可以做到这一点?
c# ×10
collections ×4
.net ×3
dictionary ×2
clone ×1
covariance ×1
enums ×1
generics ×1
immutability ×1
java ×1
list ×1
readonly ×1
t4 ×1