I read some code the other day that made me do a double take.
if(businessObject.Id.Equals(new Guid()))
{
// create object in db
}
The first thing I thought was "Is this even possible? Will this code ever run?" But it will, and it does, because the default constructor on a Guid returns Guid.Empty. And while this is counter intuitive, it is the expected behavior of a default constructor on a value type.
The C# spec says that a default constructor is implicitly provided and cannot be overridden. And the behavior is to give a value produced by a bit pattern of all zeros. So this isn't a problem with the behavior of Guid, it's a problem with the behavior of the developer. Comparing to Guid.Empty is much more intuitive in terms of expected behavior.
if (businessObject.Id.Equals(Guid.Empty))
{
// create object in db
}
Please think about the poor souls who have to read your code after you are done creating it.