iequalitycomparer

Subclass HashSet so that it always uses a certain IEqualityComparer when used in another set

孤者浪人 提交于 2019-12-11 02:25:28
问题 I want to subclass HashSet<Point> so that it uses HashSet<Point>.CreateSetComparer() as an IEqualityComparer whenever I use it inside another set. Basically every time I do this: var myDict = new Dictionary<MySubclassOfHashSet, Char>(); I want it automatically treated as : var myDict = new Dictionary<HashSet<Point>, Char>(HashSet<Point>.CreateSetComparer()); As per this question. I have currently done this manually as follows: class MySubclassOfHashSet: HashSet<Point> { public override bool

How do you get WCF serialization to preserve a non-default Comparer on a generic Dictionary?

半城伤御伤魂 提交于 2019-12-10 23:34:15
问题 Suppose we start from scratch in Visual Studio 2010 and add a 'WCF Service Aplication'. We add this method and implementation: // (in IService1.cs) [OperationContract] Dictionary<string, string> GetDictionary(); // (in Service1.svc.cs) public Dictionary<string, string> GetDictionary() { return new Dictionary<string, string>( StringComparer.InvariantCultureIgnoreCase); } Then we add a new 'Console Application' to the same solution, add a Service Reference to our service project (using all the

How to get raw hash code from a class that re-implements GetHashCode?

孤人 提交于 2019-12-10 10:17:46
问题 Short question: How do I get the object.GetHashCode() value for an object that has re-implemented GetHashCode() ? Long story: So I have about a hundred thousand objects, each sharing many (non-compile time) common strings. Common as in if the value is equal, it is the same instance. Knowing that, I figure I'd rather use a standard object comparison ( ReferenceEquals ) rather than a full string compare - particularly as these are looked up in dictionaries on a fairly regular basis. So I

What is the difference between using IEqualityComparer and Equals/GethashCode Override?

爱⌒轻易说出口 提交于 2019-12-09 08:25:35
问题 When i am using dictionaries sometimes I have to change the default Equals meaning in order to compare Keys. I see that if I override the Equals and GetHashCode on the key's class or i create a new class which implements IEqualityComparer I have the same result. So what's the difference between using IEqualityComparer and Equals/GethashCode Override? Two Examples: class Customer { public string name; public int age; public Customer(string n, int a) { this.age = a; this.name = n; } public

Dictionary using is custom key but key is always unequal

。_饼干妹妹 提交于 2019-12-09 07:31:24
I am using RTBTextPointer as custom key in dictionary... Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary<RTBTextPointer, SpintaxEditorProperties.SpintaxMappedValue>(new RTBTextPointerComparer()); I worte this RTBTextPointer, and RTBTextPointerComparer classes in class library and using this in different wpf projects, if (Init.SpintaxEditorPropertyMain.SpintaxListDict.ContainsKey(_index) == false) { Init.SpintaxEditorPropertyMain.SpintaxListDict.Add(_index,_SpintaxMappedVal); } everytime containsKey returns false, even it contains, so duplication entry occurs in dictionary.. is

IEqualityComparer<double> with a tolerance; how to implement GetHashCode?

痞子三分冷 提交于 2019-12-08 18:51:02
问题 I'm implementing a reusable DoubleEqualityComparer (with a custom tolerance: the "epsilon" constructor parameter) to ease the usage of LINQ with sequences of double. For example: bool myDoubleFound = doubles.Contains(myDouble, new DoubleEqualityComparer(epsilon: 0.01)); What is the right way to implement GetHashCode? Here's the code: public class DoubleEqualityComparer : IEqualityComparer<double>, IEqualityComparer<double?> { private readonly double epsilon; public DoubleEqualityComparer

Best way to compare two Dictionary<T> for equality

元气小坏坏 提交于 2019-12-07 06:43:12
问题 Is this the best way to create a comparer for the equality of two dictionaries? This needs to be exact. Note that Entity.Columns is a dictionary of KeyValuePair(string, object) : public class EntityColumnCompare : IEqualityComparer<Entity> { public bool Equals(Entity a, Entity b) { var aCol = a.Columns.OrderBy(KeyValuePair => KeyValuePair.Key); var bCol = b.Columns.OrderBy(KeyValuePAir => KeyValuePAir.Key); if (aCol.SequenceEqual(bCol)) return true; else return false; } public int GetHashCode

Utilizing the GetHashCode() part of IEqualityComparer<T> for direct comparisons?

最后都变了- 提交于 2019-12-06 07:53:35
问题 I've written a class deriving from IEqualityComparer<T> which works great for the LINQ query I needed it for. As I understand it, GetHashCode() (fast) is called first, then Equals() (slightly slower) if the hashcode is the same, for such operations. However when using it for direct comparisons, manually, I'm using something like return new MyIEqualityComparer().Equals(objA,objB); Which forgoes the faster GetHashCode() equality check. Is there a way of comparing objA to objB which doesn't

How to get raw hash code from a class that re-implements GetHashCode?

你离开我真会死。 提交于 2019-12-06 03:34:15
Short question: How do I get the object.GetHashCode() value for an object that has re-implemented GetHashCode() ? Long story: So I have about a hundred thousand objects, each sharing many (non-compile time) common strings. Common as in if the value is equal, it is the same instance. Knowing that, I figure I'd rather use a standard object comparison ( ReferenceEquals ) rather than a full string compare - particularly as these are looked up in dictionaries on a fairly regular basis. So I declare a class ReferenceEqualityComparer : IEqualityComparer to use with a Dictionary<string, TValue> ,

Why we need the IEqualityComparer,IEqualityComparer<T> interface?

断了今生、忘了曾经 提交于 2019-12-06 01:59:43
问题 the 'Equal' and 'GetHashcode' method are exist in the object class, and our type inherit the object base class. what's the different between implement the two methods of the object directly and using the IComparer interface? if we overriding object's Equal and GetHashCode , and push to a hashtable , it will use the overring 's equal method? what' the differents of new a hashtable with the IEqualityComparer constructor? 回答1: The IComparable interface is used when you need to be able to "sort"