tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

MailAddress class is driving me crazy

14 May 2013 1 mins .NET, Best practice or not?, Email

I’m now working a lot with emails and email addresses. Loading, parsing and so on. Because of that I’m also using a MailAddress class a lot. And I hate it. The class, even simple, makes me want to scream.

Consider simple code like this.

var mail1 = new MailAddress("mail@example.com");
var mail2 = new MailAddress("MAIL@example.com");
Console.WriteLine("Equals?:\t{0}", mail1.Equals(mail2));
Console.WriteLine("Equals?:\t{0}", mail2.Equals(mail1));
Console.WriteLine("GetHashCode1:\t{0}", mail1.GetHashCode());
Console.WriteLine("GetHashCode2:\t{0}", mail2.GetHashCode());

What do you think the output be? Drum roll please…

Equals?:        True
Equals?:        True
GetHashCode1:   582340455
GetHashCode2:   1045083261

Why the hell somebody did the work overriding the Equals and not providing the implementation of GetHashCode that aligns with that?

I think it’s time to implement my own. 😎

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.