我正在尝试利用 SortedDicationary 的异常功能,但我快疯了,因为它的行为不像我预期的那样。只要字典的条目是简单数据类型,Excpet 函数就可以工作。但是,我想将对象存储在字典中。
这是我的示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DictionaryTest
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<string, Object> objects = new SortedDictionary<string, Object>();
SortedDictionary<string, Object> objects2 = new SortedDictionary<string, Object>();
objects.Add("A", new Object());
objects.Add("B", new Object());
objects2.Add("A", new Object());
IEnumerable<KeyValuePair<string, Object>> objects_a_only = objects.Except(objects2);
foreach (KeyValuePair<string, Object> o in objects_a_only)
{
Console.WriteLine(o.Key);
}
Console.WriteLine("Program finished. Press any key to continue");
Console.ReadKey();
}
}
}
我期望只得到'A'作为输出,但它实际上返回'A'和'B'。
据我了解,匹配仅根据每个条目的键进行。所以我看不出有任何理由使用定制的比较类。
对此有什么想法吗?