小编Rip*_*rot的帖子

C#Dictionary中的KeyNotFoundException在根据内容更改属性值后,计算GetHashCode.为什么?

请参阅下面的代码.

            static void Main(string[] args)
            {
    // Create Dictionary
                var dict = new Dictionary<TestClass, ValueClass>();

    // Add data to dictionary
                CreateSomeData(dict); 

    // Create a List
                var list = new List<TestClass>();
                foreach(var kv in dict) {
    // Swap property values for each Key
    // For example Key with property value 1 will become 6
    // and 6 will become 1
                    kv.Key.MyProperty = 6 - kv.Key.MyProperty + 1;

    // Add the Key to the List
                    list.Add(kv.Key);
                }

// Try to print dictionary …
Run Code Online (Sandbox Code Playgroud)

c# hash dictionary

2
推荐指数
1
解决办法
257
查看次数

标签 统计

c# ×1

dictionary ×1

hash ×1