gethashcode

String.GetHashCode() returns different values

北城以北 提交于 2019-12-21 03:19:25
问题 Why is GetHashCode() returning a different value for the same string? I can't describe how to duplicate this, but trust that this is not a practical joke and that the two following lines came from my watch window at two separate times: "DDD.Events.Application.ApplicationReferenceCreated".GetHashCode() -1386151123 int "DDD.Events.Application.ApplicationReferenceCreated".GetHashCode() 1858139950 int How could this happen? I don't know if this helps, but I am running on .NET 4.0 in VS 2010 and I

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

…衆ロ難τιáo~ 提交于 2019-12-20 05:28:04
问题 I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable<T> interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal other As Address) As Boolean _ Implements System.IEquatable(Of Address).Equals If ReferenceEquals(Me, other) Then Return True Return AddressId = other.AddressId End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If obj Is

Does this solve Nhibernate identity problem and GetHashCode issues?

二次信任 提交于 2019-12-20 03:35:14
问题 The solution I propose involves quite a bit of code, but you can just copy it all and past it in a VS test solution assuming you have SqLite installed, and you should be able to run the tests yourself. As I have been struggling with the object identity versus object equality and database identity problem using Nhibernate, I have read various posts. However, I could not get a clear picture of how to properly set up object identity in conjunction with collections. Basically, the big problem, as

General advice and guidelines on how to properly override object.GetHashCode()

家住魔仙堡 提交于 2019-12-17 08:29:49
问题 According to MSDN, a hash function must have the following properties: If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values. The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals

What are the rules I should follow to ensure GetHashCode() method returns unique value for an object?

时光总嘲笑我的痴心妄想 提交于 2019-12-13 22:19:46
问题 What are the rules I should follow to ensure GetHashCode() method returns unique value for an object? For example: Should I include some prive members for the calculation? Should I multiply instead of sum? Can I be sure that I am generating a uniqe hash code for a particular object graph? etc. 回答1: You shouldn't even aim for GetHashCode() returning a unique value for each object. That's not the point of GetHashCode() . Eric Lippert has a great post about hash codes which you should read

Equals method implementation helpers (C#)

删除回忆录丶 提交于 2019-12-12 07:54:28
问题 Everytime I write some data class, I usually spend so much time writing the IEquatable implementation. The last class I wrote was something like: public class Polygon { public Point[] Vertices { get; set; } } Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the reverse order, and that adds a lot of complexity to the Equals method. After many unit tests, and corresponding implementation, I gave up, and changed my application to

How to implement GetHashCode() in a C# struct [duplicate]

白昼怎懂夜的黑 提交于 2019-12-12 00:54:20
问题 This question already has answers here : What is the best algorithm for overriding GetHashCode? (19 answers) Closed 4 years ago . 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 FolderID; public MyEnum SubItemKind; public int SubItemID; } What is the right way to implement the GetHashCode() method? a) return FolderID ^

GetHashCode Equals implementation for a class in C#

ぃ、小莉子 提交于 2019-12-11 20:38:42
问题 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 Person { string Name string Email public override Equals(object obj) { if (ReferenceEquals(obj, null)) return false; if (ReferenceEquals(this, obj)) return true; if (obj is Person) { Person person = (Person)obj; return (this.Name == person.Name) ||

Overloading == operator for class containing only string attributes

♀尐吖头ヾ 提交于 2019-12-11 11:57:32
问题 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 { get; set; } public string ThirdAttribute { get; set; } public string FourthAttribute { get; set; } public string FifthAttribute { get; set; } } I know how to overload the operator itself, however, I am wondering about the following points: Is there a way

How to implement GetHashCode for this situation?

我怕爱的太早我们不能终老 提交于 2019-12-11 09:31:13
问题 I'm trying to implement an IEqualityComparer<string> 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, string y) { return x.StartsWith(y) || y.StartsWith(x); } public int GetHashCode(string obj) { return obj.GetHashCode(); } Ofcourse implementing the Equals method is pretty easy.But the GetHashCode is not, I couldn't think any way to implement it