30.06.2013 Views

Under the Hood of .NET Memory Management - Simple Talk

Under the Hood of .NET Memory Management - Simple Talk

Under the Hood of .NET Memory Management - Simple Talk

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Common <strong>Memory</strong> Problems<br />

public partial struct GeographicPoint<br />

{<br />

public static bool operator ==(GeographicPoint first, GeographicPoint second)<br />

{<br />

return first.Equals(second);<br />

}<br />

public static bool operator !=(GeographicPoint first, GeographicPoint second)<br />

{<br />

return !first.Equals(second);<br />

}<br />

public override int GetHashCode()<br />

{<br />

return (int)this.latitude ^ (int)this.longitude ^ this.altitude;<br />

}<br />

public override bool Equals(object obj)<br />

{<br />

if (obj is GeographicPoint)<br />

{<br />

return Equals((GeographicPoint)obj);<br />

}<br />

}<br />

return false;<br />

public bool Equals(GeographicPoint o<strong>the</strong>r)<br />

{<br />

return this.latitude == o<strong>the</strong>r.latitude<br />

&& this.longitude == o<strong>the</strong>r.longitude<br />

&& this.altitude == o<strong>the</strong>r.altitude;<br />

}<br />

}<br />

Listing 4.34: A fully implemented GeographicPoint optimized for hashing and safe from boxing errors.<br />

With <strong>the</strong> struct fully implemented, we now have more useful semantics. We can<br />

now ensure GeographicPoint is thread-safe, Hashtable friendly, and works with<br />

equality operators.<br />

116

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!