Quoting from an answer from this question.
Guid is a value type, so a variable of type Guid can't be null to start with.
What then if I see this?
public Nullable<System.Guid> SomeProperty { get; set; }
how should I check if this is null? Like this?
(SomeProperty == null)
or like this?
(SomeProperty == Guid.Empty)
You can compare a GUID with the value of the Guid. Empty field to determine whether a GUID is non-zero. The following example uses the Equality operator to compare two GUID values with Guid. Empty to determine whether they consist exclusively of zeros.
Like other value types, GUID also has a nullable type which can take null value.
You can use these methods to get an empty guid. The result will be a guid with all it's digits being 0's - " 00000000-0000-0000-0000-000000000000 ". In newer C# versions, default(Guid) and default are the same, too.
Empty is "{00000000-0000-0000-0000-000000000000}" which located the representation range of a guid, but we just marked is as Empty, so it is safe to use (someGuid ==Guid. Empty).
If you want be sure you need to check both
SomeProperty == null || SomeProperty == Guid.Empty
Because it can be null 'Nullable' and it can be an empty GUID something like this {00000000-0000-0000-0000-000000000000}
SomeProperty.HasValue I think it's what you're looking for.
See DevDave's or Sir l33tname's answer instead.
EDIT : btw, you can write System.Guid?
instead of Nullable<System.Guid>
;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With