当我尝试创建字典时遇到问题,其中键是 System.Enum。问题是,像这样的字典会分配垃圾,因为默认的 EqualityComparer 不是最好的之一。我尝试编写自己的比较器,但没有成功。这有可能吗?
public enum MyEnum
{
One, Two, Three
}
public Dictionary<Enum, string> dict = new Dictionary<Enum, string>();
public void Test()
{
this.dict.Add(MyEnum.One, "One");
this.dict.Add(MyEnum.Two, "Two");
this.dict.Add(MyEnum.Three, "Three");
string result;
this.dict.TryGetValue(MyEnum.Two, out result); // <-- memory alocation :-(
}
Run Code Online (Sandbox Code Playgroud)