All Questions

1127
votes
16answers
144420 views

What is the best algorithm for an overridden System.Object.GetHashCode?

In .NET System.Object.GetHashCode method is used in a lot of places, throughout the .NET base class libraries. Especially when finding items in a collection fast or to determine equality. Is there a ...
120
votes
4answers
40464 views

Default implementation for Object.GetHashCode()

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 ...
18
votes
5answers
6241 views

Why is ValueType.GetHashCode() implemented like it is?

From ValueType.cs **Action: Our algorithm for returning the hashcode is a little bit complex. We look ** for the first non-static field and get it's hashcode. If the type has no ** ...
43
votes
2answers
20741 views

How is GetHashCode() of C# string implemented?

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 ...
91
votes
11answers
82729 views

.NET unique object identifier

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 t...
37
votes
4answers
12538 views

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

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 Custome...
20
votes
4answers
14080 views

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

EnumerableObject : IEnumerable<Foo> wraps a List<Foo> If EnumerableObject a.SequenceEquals( EnumerableObject b), then they are equal. Therefore, a GetHashCode must be implemented. The p...
31
votes
4answers
9039 views

What's the best strategy for Equals and GetHashCode?

I'm working with a domain model and was thinking about the various ways that we have to implement this two methods in .NET. What is your preferred strategy? This is my current implementation: pu...
108
votes
3answers
22683 views

What's the role of GetHashCode in the IEqualityComparer<T> in .NET?

I'm trying to understand the role of the GetHashCode method of the interface IEqualityComparer. The following example is taken from MSDN: using System; using System.Collections.Generic; class Exampl...
12
votes
4answers
6672 views

When Should a .NET Class Override Equals()? When Should it Not?

The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states in part Overriding operator == in non-immutable types is not recommended. The newer .NE...

Previous Next