Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • What is special about the GetHashCode implementation of an anonymous type that it is able to avoid collisions? Commented Oct 8, 2013 at 14:52
  • @Lukazoid It doesn't avoid collisions entirely, it's just a type that has a sensibly written GetHashCode method that is based on the hashes of the underlying property values. It's likely to be more evenly distributed than a more naive implementation that you would write, and doesn't have as large of risks of making a mistake in the math that causes problems. Commented Oct 8, 2013 at 14:54
  • @Lukazoid: I've deleted that part since i guess that it also can produce collisions (f.e. if it overflows) since it uses the same algorithm as above. Commented Oct 8, 2013 at 14:54
  • 2
    @TimSchmelter It doesn't use exactly the same algorithm. It uses larger prime numbers and as such will be more likely to be evenly distributed over the range of an int. Of course there are obviously more possible values than there are integers, so avoiding collisions entirely is clearly impossible, they can only be minimized. Commented Oct 8, 2013 at 14:59
  • I think your return value on the second example should read return new[] { a, b, c }.GetHashCode();. Without the braces ([]), I get "Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access." Commented Jun 19, 2017 at 20:03