gethashcode

What's the best strategy for Equals and GetHashCode?

允我心安 提交于 2019-11-26 08:27:33
问题 I\'m working with a domain model and was thinking about the various ways that we have to implement these two methods in .NET. What is your preferred strategy? This is my current implementation: public override bool Equals(object obj) { var newObj = obj as MyClass; if (null != newObj) { return this.GetHashCode() == newObj.GetHashCode(); } else { return base.Equals(obj); } } // Since this is an entity I can use its Id // When I don\'t have an Id, I usually make a composite key of the properties

Good GetHashCode() override for List of Foo objects respecting the order

寵の児 提交于 2019-11-26 06:37:58
问题 EnumerableObject : IEnumerable<Foo> wraps a List<Foo> If EnumerableObject a.SequenceEquals( EnumerableObject b) , then they are equal. Therefore, a GetHashCode must be implemented. The problem is XORing each element in the list will return the same hash code for any list with all and only the same elements, regardless of order. This is Okay in terms of it working, but will result in many collisions, which will slow down retrieval, etc. What is a good, fast GetHashCode method for lists of

How is GetHashCode() of C# string implemented?

依然范特西╮ 提交于 2019-11-26 06:37:46
问题 I\'m just curious because I guess it will have impact on performance. Does it consider the full string? If yes, it will be slow on long string. If it only consider part of the string, it will have bad performance (e.g. if it only consider the beginning of the string, it will have bad performance if a HashSet contains mostly strings with the same. 回答1: Be sure to obtain the Reference Source source code when you have questions like this. There's a lot more to it than what you can see from a

How do I create a HashCode in .net (c#) for a string that is safe to store in a database?

China☆狼群 提交于 2019-11-26 06:37:30
问题 To quote from Guidelines and rules for GetHashCode by Eric Lippert: Rule: Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains Suppose you have a Customer object that has a bunch of fields like Name, Address, and so on. If you make two such objects with exactly the same data in two different processes, they do not have to return the same hash code. If you make such an object on Tuesday in one process, shut it down, and run the program again on Wednesday,

.NET unique object identifier

穿精又带淫゛_ 提交于 2019-11-26 05:18:40
问题 Is there a way of getting a unique identifier of an instance? GetHashCode() is the same for the two references pointing to the same instance. However, two different instances can (quite easily) get the same hash code: Hashtable hashCodesSeen = new Hashtable(); LinkedList<object> l = new LinkedList<object>(); int n = 0; while (true) { object o = new object(); // Remember objects so that they don\'t get collected. // This does not make any difference though :( l.AddFirst(o); int hashCode = o

Correct way to override Equals() and GetHashCode() [duplicate]

浪尽此生 提交于 2019-11-26 01:07:51
问题 This question already has an answer here: What's the best strategy for Equals and GetHashCode? 5 answers I have never really done this before so i was hoping that someone could show me the correct what of implementing a override of Except() and GetHashCode() for my class. I\'m trying to modify the class so that i can use the LINQ Except() method. public class RecommendationDTO{public Guid RecommendationId { get; set; } public Guid ProfileId { get; set; } public Guid ReferenceId { get; set; }

Default implementation for Object.GetHashCode()

你说的曾经没有我的故事 提交于 2019-11-26 00:52:46
问题 How does the default implementation for GetHashCode() work? And does it handle structures, classes, arrays, etc. efficiently and well enough? I am trying to decide in what cases I should pack my own and in what cases I can safely rely on the default implementation to do well. I don\'t want to reinvent the wheel, if at all possible. 回答1: namespace System { public class Object { [MethodImpl(MethodImplOptions.InternalCall)] internal static extern int InternalGetHashCode(object obj); public

What is the best algorithm for overriding GetHashCode?

。_饼干妹妹 提交于 2019-11-25 21:33:52
问题 In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when determining equality. Is there a standard algorithm or best practice on how to implement GetHashCode for my custom classes so I don\'t degrade performance? 回答1: I usually go with something like the implementation given in Josh Bloch's fabulous Effective Java. It's fast and creates a pretty good hash

Default implementation for Object.GetHashCode()

为君一笑 提交于 2019-11-25 18:52:45
How does the default implementation for GetHashCode() work? And does it handle structures, classes, arrays, etc. efficiently and well enough? I am trying to decide in what cases I should pack my own and in what cases I can safely rely on the default implementation to do well. I don't want to reinvent the wheel, if at all possible. David Brown namespace System { public class Object { [MethodImpl(MethodImplOptions.InternalCall)] internal static extern int InternalGetHashCode(object obj); public virtual int GetHashCode() { return InternalGetHashCode(this); } } } InternalGetHashCode is mapped to