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
0
votes
4 answers

GetHashCode Equals implementation for a class in C#

I have a class Person for which I have to override the Equals and GetHashCode method. Two person objects are equals if the Name matches OR if the Email matches. What's a good way of doing this with a considerably efficient hash function? class…
hIpPy
  • 4,649
  • 6
  • 51
  • 65
0
votes
0 answers

GetHashCode Implemetation for multiple properties in class

I need to compare to List's of Gu45Entity List Gu45EntityItemsFromDb = DataBaseModel.Instance.Gu45DocumentStore.Include(x => x.EGu45.Gu45).ToList(); List temp =…
A191919
  • 3,422
  • 7
  • 49
  • 93
0
votes
0 answers

Trying to set up a CompareLists method with an overridden Equals/Gethash code methods

I'm trying to set up a way to compare some nested lists with objects that I'm importing from MongoDB. I have already set up the lists object: public class SecurityGroup { public ObjectId Id { get; set; } public string GroupID { get; set; } …
0
votes
0 answers

Strange result from GetHashCode

class Program { static void Main(string[] args) { new Wtf { Foo = "FOO" }.GetHashCode(); } private struct Wtf { public string Foo { get; set; } public override int GetHashCode() => new { A = Foo…
ConditionRacer
  • 4,418
  • 6
  • 45
  • 67
0
votes
1 answer

GetHashCode - Generate hash code from 64-bit address

We've got a 32-bit C++/CLI assembly and one of the classes wraps a native object. It overrides GetHashCode to return the address of the native object it wraps (m_native is a pointer): int NativeWrapper::GetHashCode() { return…
nblackburn
  • 338
  • 2
  • 10
0
votes
2 answers

Converting url to hash code for Image using C#

For my Project I need to convert the Image from Url to hash code. Like "CE3222F5.jpg". But I am not getting where should I implement Gethashcode method in my code. My code to get the image from url is as Poi.Images = new List { new…
harry.luson
  • 247
  • 7
  • 19
0
votes
1 answer

key.GetHashCode() returns key

I am implementing a hashtable and I use GetHashCode to get a unique hashcode for each key I'm using, however, when I call key.GetHashCode(), the function returns the key. After I use the modulo to get the right bucket of the hashtable, we are able…
Denis
  • 664
  • 9
  • 24
0
votes
0 answers

Reimplementing GetHashCode()

I'm writing a profile editor in Visual Studio 2015 for a game that was written in Unity. That means that my String.GetHashCode() and the games String.GetHashCode() are not the same. The idea that I have is to simply find the games method and then…
Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
0
votes
1 answer

is this GetHashCode good enough?

I have this class: public class RelationInfo { public FieldInfo Field; public int Index; public override int GetHashCode() { return Field.GetHashCode() ^ int.GetHashCode(); } public override bool Equals(object obj) { return…
Jack Lu
  • 139
  • 2
  • 12
0
votes
1 answer

How to implement GetHashCode() in a C# struct

I have a struct that overrides the Equals() method and the compiler complains about GetHashCode() not being overridden. My struct: private struct Key { ... public override int GetHashCode() { return ?; } public int…
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
0
votes
3 answers

Avoiding duplicates in a HashSet of custom types in C#

I have the following custom class deriving from Tuple: public class CustomTuple : Tuple, DateTime?> { public CustomTuple(IEnumerable strings, DateTime? time) : base(strings.OrderBy(x => x).ToList(), time) { } } and…
rleelr
  • 1,854
  • 17
  • 26
0
votes
0 answers

How can I tell if my GetHashCode() function is suitable?

Currently, I have the following two enum types: [Flags] public enum KeyboardLocks : byte { None = 0, NumLock = 1 << 0, CapsLock = 1 << 1, ScrollLock = 1 << 2 } [Flags] public enum KeyboardModifiers : byte { …
Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
0
votes
2 answers

C#, Which class fields/members should be considered when overriding GetHashCode and Equals?

There is this excelent question and answer about this topic: Do I HAVE to override GetHashCode and Equals in new Classes? As it mentions: you only need to override them if you need value equality semantics. The System.Object implementation isn't…
Alberto Montellano
  • 5,886
  • 7
  • 37
  • 53
0
votes
1 answer

ErrorProvider with custom GetHashCode

I have a form responsible of creating (and saving) new Patients. On this form I am using an ErrorProvider to show error icons on invalid fields (in this case just "LastName"). So, as usual => errorProvider.DataSource = patient; Everything works fine…
Marco
  • 141
  • 1
  • 1
  • 7
0
votes
2 answers

64bit HashCodes, IEqualityComparer & Intersect/Except

I'm generating 64 bit hashcodes from strings, and storing this value in a database Is it possible to override GetHashCode with a 64 bit long type instead of 32 byte int? If this is not possible, is it possible to implement Equals and GetHashCode…
mrb398
  • 1,277
  • 4
  • 24
  • 32