Search code examples
c#boxingintptriequatable

Why does IntPtr not implement IEquatable<IntPtr>?


I was seeing a rather large amount of garbage collector stalls in my application, so I profiled it, and saw that a lot of garbage was being generated by a method of mine that did nothing more than this:

return Address.Equals(other.Address)

Where Address is an IntPtr.

Turns out that IntPtr does not implement IEquatable<T> like most value types, and therefore I was boxing other.Address each time.

Is there any good reason for IntPtr not implementing that interface, or is this an oversight?


Solution

  • It is an oversight. IEquatable<T> was added in .NET 2.0 after IntPtr existed. There are a few places in the BCL that were not upgraded properly and nobody seems interested in fixing them.

    Just use ==. It is better style anyway because it clearly shows your intent.