This tests to see if it has a value (is not null), and if it does, I can set that back to myGuid
. If it doesn't, I just set myGuid
to a new GUID (00000000-0000-0000-0000-000000000000):
Guid myGuid = SomeProperty.HasValue ? (Guid)SomeProperty : Guid.NewGuid();
You can also do
Guid myGuid = SomeProperty ?? Guid.Empty;
In these ways, you can convert a Guid?
to a Guid
. And for those who want to downvote this, there's no reason to as this code WORKS. Give an explanation instead of your hit-and-runs.