A few days back, I came across an article by Bill Wagner, on the topic of validating the state of your objects. His approach, in essence, is to override the bool operator on your class, and use it to return a boolean indicating whether the instance is in a valid state. Simplified to the extreme, the code would look something like this:
public class Person
{
public string Name
{ get; set; }
public static implicit operator bool( Person person )
{
return ( person.Name.Length > 0 );
}
}
Regardless of what I think of the approach, I was initially puzzled by the line:
public static implicit operator bool( Person person )
I had never encountered the keyword "implicit" before in C#, and was therefore not too sure of what was happening there.
More...