The Mysterious System.Guid
I have some objects I am serializing that have an ID property. The ID property is a GUID and I got to looking at the nature of the System.Guid class during this project.
I gots me some questions.
Constructor Madness
Why is there a default constructor AND a static creator? Both of the following code lines are valid.
this.ID = new Guid();
and
this.ID = Guid.NewGuid();
Also available is the Guid.Empty field, which is a GUID initialized to all zero values. It turns out, you get an “empty” GUID when using the default constructor instead of the static initializer. This causes us to do things like check parameter GUIDs for emptiness in function parameter checking. That’s a little irritating and would be wholly unnecessary if the new Guid() return an actual initialized GUID.
I am convinced that the reason new Guid() exists is to support serialization. Without the default constructor, we could not serialize the object. OK, fine. Then why the static initializer? Why not just return a real GUID from the default constructor? There isn’t a factory pattern at work here. I don’t get it. Anyone?


