Search code examples
c#.netguiddefault-constructor

Why does the parameterless Guid constructor generate an empty GUID?


Why does the parameterless Guid constructor generate an empty GUID rather than default to a generated one as with Guid.NewGuid()?

Is there a particular use for an empty Guid?


Solution

  • Why does the parameterless Guid constructor generate an empty GUID rather than default to a generated one as with Guid.NewGuid()?

    Short answer: Because the language/runtime didn't let the designer of the Guid type define a default constructor.

    It's not only conventional that the value of a "default-constructed" struct is zero, you simply cannot define a default constructor for a struct. When you say new Guid() the runtime gives you a new object where all the fields are initialized to their default values: http://msdn.microsoft.com/en-us/library/ah19swz4%28VS.71%29.aspx

    Some rationale can be found here: https://web.archive.org/web/20131102220804/http://www.yoda.arachsys.com/csharp/faq/#struct.constructors