Questions tagged [gethashcode]

GetHashCode is method of base Object class of .Net Framework.

GetHashCode is method of base Object class of .Net Framework. A hash code is a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection. The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table.

Important notes

  • Equality of GetHashCode value doesn't necessarily imply equality of objects. The equality must be verified using Equals.
  • Inequality of GetHashCode necessarily means inequality of objects.
344 questions
1
vote
2 answers

How to implement IEquatable when mutable fields are part of the equality - Problem with GetHashCode

I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
1
vote
2 answers

How to implement GetHashCode for this situation?

I'm trying to implement an IEqualityComparer which basically compares two strings in a way that,(let's assume we have two strings x and y) if x starts with y or y starts with x they should be treated as equal. public bool Equals(string x,…
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1
vote
2 answers

Overloading == operator for class containing only string attributes

What would be the best (most elegant or performing) way of overloading the equality operator on a class containing only string attributes? Example: class MagicClass { public string FirstAttribute { get; set; } public string SecondAttribute {…
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
1
vote
2 answers

Retrieve Id from DB by using Equal/GetHashCode in Where clause

Currently I'm doing the following to retrieve the ID of a stored object in the DB which match some fields of the entity MyObject MyObject contract = new MyObject( some parameters ); Session.Query().Where( x=>x.Field1==contract.Field1 &&…
qwark
  • 493
  • 1
  • 4
  • 15
1
vote
3 answers

Getting KeyNotFoundException when using key previously retrieved from key collection?

I've got the following code where for some reason I'm getting a KeyNotFoundException even though I'm using a key that I had retrived a couple of lines above. Does anyone know of a situation where this wouldn't work? I'm stumped. BTW…
Martin MacPherson
  • 337
  • 1
  • 2
  • 10
1
vote
0 answers

System.Windows.Foms.HtmlElement Equals

I have a program that loads a web page and walks the DOM creating a HtmlElement to map (using Dictionary). I then wait for the user to click and element and get the HtmlElement using webBrowser.Document.GetElementFromPoint. I then try to look up…
Tom West
  • 1,759
  • 2
  • 13
  • 20
1
vote
1 answer

Is there any reason not to generate conditional hash codes using GetHashCode in .NET?

In a class I use as a document or document+page identifier, I use the following implementation of GetHashCode. It 'felt' right but since I haven't really seen domain-specific conditioning in this method before, I was wondering if there is a reason…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
1
vote
2 answers

How to override GetHashCode when a Date matches if plus or minus seven days

If I have the following methods: public bool Equals(VehicleClaim x, VehicleClaim y) { bool isDateMatching = this.IsDateRangeMatching(x.ClaimDate, y.ClaimDate); // other properties use exact matching } private bool…
davy
  • 4,474
  • 10
  • 48
  • 71
1
vote
2 answers

Dictionary using is custom key but key is always unequal

I am using RTBTextPointer as custom key in dictionary... Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary(new RTBTextPointerComparer()); I worte this RTBTextPointer, and…
Jay
  • 33
  • 5
1
vote
2 answers

Which methods internally call GetHashCode?

I am aware of the importance to override GetHashCode when we override Equals method. I assume Equals internally calls GetHashCode. What are the other methods that might be internally using GetHashCode?
Carbine
  • 7,849
  • 4
  • 30
  • 54
1
vote
3 answers

Can't override GetHashCode (cannot change return type when overriding method int UnityEngine.Object.GetHashCode")

I'm using C# in Unity3D game engine. in my MonoBehaviour script I need to override GetHashCode. But whenever I do, I get the error cannot change return type when overriding method int UnityEngine.Object.GetHashCode" (Isn't GetHashCode located inside…
vexe
  • 5,433
  • 12
  • 52
  • 81
1
vote
1 answer

Is there a built-in IEqualityComparer that compares objects only using their hash value?

Is there a built-in IEqualityComparer that compares objects by the value returned by their GetHashCode value? It's easy to write, but I'd prefer to use a provided class instead of a custom one. Current code: private class HashComparer :…
mafu
  • 31,798
  • 42
  • 154
  • 247
1
vote
1 answer

CompositeId and overrided GetHashCode() and Equals() methods

I have entity class: [Serializable, Class(Table = "mon.tableView", Lazy = false)] public class TableView { [CompositeId(1)] [KeyProperty(2, Name = "column1", Column = "column1", TypeType = typeof(int))] [KeyProperty(3, Name =…
maciusik
  • 103
  • 1
  • 1
  • 9
1
vote
1 answer

Testing Collections of Objects for Equality using IEquatable

I have a class that I have made implement IEquatable so that when I'm testing I can easily compare IEnumerable collections of those objects using a call such as: Assert.IsTrue(expected.SequenceEqual(actual)); This is currently working well but I…
davy
  • 4,474
  • 10
  • 48
  • 71
1
vote
4 answers

How should I implement IEqualityComparer.Equals

Concerning IEqualityComparer, is there ever a reason why the Equals(T x, T y) implementation should be anything other than what I have below? public class MyEquality : IEqualityComparer { //My Equals(T x, T y) always looks like this …
user2023861
  • 8,030
  • 9
  • 57
  • 86