For a project i am working on, i wrote a WebSite class, which will hold information about a web site. In the database the web sites should be unique, and by business rules uniquness is defined as, no same name web site shall exist twice, no same urls shall exist twice; so in my website class i overloaded == sign to check for these two properties such as :
public static bool operator == (WebSite site1,WebSite site2)
{
return site1.Equals(site2);
}
public override bool Equals(object obj)
{
WebSite temp = (WebSite)obj;
if(this.Name==temp.Name) return true;
if(this.Url==temp.Url) return true;
return false;
}
[/code]
At the first glance, the code seemed right to me; however what is WebSite2 is null, so we should check it right ? i changed the code as below:
and... my code broke. Himm where was the problem ? there is a recursive call in the null check :) so i have to typecast upto object before i do the null check, or before typecasting it to website i have to do the null check such as :
By the way i catch this error with unit testing :))