gethashcode

Proper way to write GetHashCode() when Equality Comparer is based on OR operation?

可紊 提交于 2019-12-11 07:39:14
问题 I'm trying to write an Equality Comparer for a simple class with 3 fields, like so: public class NumberClass { public int A { get; set; } public int B { get; set; } public int C { get; set; } } My condition for two objects of NumberClass to be equal is if Obj1.A == Obj2.A || Obj1.B == Obj2.B (in other words, OR), Obj1 and Obj2 being instances of NumberClass . I can easily write the Equals() of my comparer as follows, but I don't know what to do with my GetHashCode() method. public bool Equals

C#: override GetHashCode, what does this code do?

本秂侑毒 提交于 2019-12-11 03:21:59
问题 Here's the code I found in the Nhibernate 3 Beginners Guide to override GetHashCode . I don't understand why it uses result * 397. If 397 just a random number he use to generate unique result?? Can we just GetHashCode for firstname, middlename and lastname, then combine it together using ^, it should also generate an unique result. public override int GetHashCode() { unchecked { var result = FirstName.GetHashCode(); result = (result*397) ^ (MiddleName != null ? MiddleName.GetHashCode() : 0);

Overloading GetHashCode and the equality operator using the XOR operator on enums

时间秒杀一切 提交于 2019-12-11 02:05:12
问题 I have the following class which is part of a statictical analysis package. The MetricKey object is used as a dictionary key. Decision , MetricUnit & Portfolio are all enums. I had to override the equality operator (==) to get dictionary key matching working. I used the guidance at http://msdn.microsoft.com/en-us/library/ms173147.aspx. The guidance said I should overload the GetHashCode method which I have done but I don't understand the implications of casting my enums to integers for the

C# performant alternatives to HashSet and Dictionary that do not use GetHashCode

痞子三分冷 提交于 2019-12-10 16:12:50
问题 I'm looking for built-in alternatives of HashSet and Dictionary objects that have better performance than lists but do not use the internal GetHashCode method. I need this because for the class I have written, there is no way of writing a GetHashCode method that fulfills the usual contract with Equals other than public override int GetHashCode() { return 0; } // or return any other constant value which would turn HashSet and Dictionary into ordinary lists (performance-wise). So what I need is

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

C# Decimal.GetHashCode() and Double.GetHashCode() equal

旧城冷巷雨未停 提交于 2019-12-08 16:23:38
问题 Why is it that 17m.GetHashCode() == 17d.GetHashCode() (m=decimal, d=double) Additionally, as expected 17f.GetHashCode() != 17d.GetHashCode() (f=float) This appears to be true for both net3.5 and net4.0. As I understand, the internal bit representations of these types are quite different. So how come that the hash codes of decimal and double types equal for equal initialization values? Is there some conversion taking place before calculation of the hash? I found that the source code for Double

GetHashCode for the object with several data members

我们两清 提交于 2019-12-08 12:39:15
问题 public class MyClass { public string x; public string y; } public class MyClassEqualityComparer : IEqualityComparer<MyClass> { public int GetHashCode(MyClass myobj) { if(myObj == null) { return base.GetHashCode(); } if (myObj.x != null && myObj.y != null) { return myObj.x.GetGashCode()^myObj.y.GetGashCode(); } } } what should be the implementation if myObj.x or/and myObj.y are nulls 回答1: The only requirement for a hash code is that two objects that are considered equal share the same hash

How to generate a unique hash for a collection of objects independent of their order [duplicate]

女生的网名这么多〃 提交于 2019-12-07 17:03:46
问题 This question already has answers here : Getting hash of a list of strings regardless of order (5 answers) Closed 5 years ago . Let's say I have a class public class MyClass { public string Type { get; set; } public int Id { get; set; } } and I have a collection class that is simply a strongly typed List public class MyClassList : List<MyClass> { public MyClassList(IEnumerable<MyClass> enumerable) : base (enumerable) {} } I want MyClassList to be able to generate a unique hash-code for

GetHashCode and Equals implementation in Dictionary C#

你。 提交于 2019-12-07 16:25:09
问题 I came to this site searching for object comparison in Dictionary, and i came to know that overriding GetHashCode and Equals are a must for doing object comparison in C#. Here is a piece of code that i have been trying to solve out, using FOREACH iteration Method. But my Boss says to do the same without using any iteration(maybe by using containskey or containsvalue method), due to performance issues. Any help is highly welcome.. public class employee { public string empname { get; set; }