67

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)
DanielV
  • 2,076
  • 2
  • 40
  • 61
Saturnix
  • 10,130
  • 17
  • 64
  • 120
  • There's an answer to that question that addresses a nullable guid - a.k.a. `Nullable` or `Guid?` – Damien_The_Unbeliever Jul 17 '13 at 07:52
  • 2
    I think this link would be helpful for you http://stackoverflow.com/questions/676078/which-is-preferred-nullable-hasvalue-or-nullable-null – sagar Jul 17 '13 at 07:55
  • helpful indeed, thanks! – Saturnix Jul 17 '13 at 08:47
  • **NOTE** - this question is specifc. about Nullabe Guid's... For how to check if a regular `Guid` is empty: http://stackoverflow.com/questions/9837602/why-isnt-there-a-guid-isnullorempty-method – Don Cheadle Dec 18 '15 at 17:45

7 Answers7

137

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}

Sir l33tname
  • 4,026
  • 6
  • 38
  • 49
  • 2
    to confirm: Guid.Empty.ToString() == "00000000-0000-0000-0000-000000000000" – Matt Kemp Apr 09 '15 at 14:30
  • 6
    Guid is never null, but it can be Guid.Empty – piris Jul 21 '15 at 09:22
  • 8
    Guid itself not but if you read the question it's a Nullable Guid and the hole point of Nullable is that it can be null ^^ – Sir l33tname Jul 21 '15 at 09:31
  • This is the correct answer; Note that if you're serializing a `Guid` from input, if you use the *non*-nullable variant, `null` will serialize as `Guid.Empty`, which can save you a `null` check here. – zcoop98 Mar 19 '21 at 23:29
28

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> ;)

derekbaker783
  • 8,109
  • 4
  • 36
  • 50
dotixx
  • 1,490
  • 12
  • 23
19

Note that HasValue will return true for an empty Guid.

bool validGuid = SomeProperty.HasValue && SomeProperty != Guid.Empty;

DevDave
  • 6,700
  • 12
  • 65
  • 99
4

Check Nullable<T>.HasValue

if(!SomeProperty.HasValue ||SomeProperty.Value == Guid.Empty)
{
 //not valid GUID
}
else
{
 //Valid GUID
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • As other people have pointed out : This will indeed tell you if the Guid is null, but beware! It will return true if the Guid has value Guid.Empty. – jeancallisti Sep 29 '22 at 16:42
3

You should use the HasValue property:

SomeProperty.HasValue

For example:

if (SomeProperty.HasValue)
{
    // Do Something
}
else
{
    // Do Something Else
}

FYI

public Nullable<System.Guid> SomeProperty { get; set; }

is equivalent to:

public System.Guid? SomeProperty { get; set; }

The MSDN Reference: http://msdn.microsoft.com/en-us/library/sksw8094.aspx

Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42
  • As other people have pointed out : This will indeed tell you if the Guid is null, but beware! It will return true if the Guid has value Guid.Empty. – jeancallisti Sep 29 '22 at 16:42
3

Beginning with C# 7.1, you can use default literal to produce the default value of a type when the compiler can infer the expression type.

Console.Writeline(default(Guid));  
   // ouptut: 00000000-0000-0000-0000-000000000000

Console.WriteLine(default(int));  // output: 0

Console.WriteLine(default(object) is null);  // output: True

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default

3

You can create a extension method to validate the GUID.

public static class Validate
{
    public static void HasValue(this Guid identity)
    {
        if (identity ==  null || identity == Guid.Empty)
            throw new Exception("The GUID needs a value");
    }
}

And use the exension

    public static void Test()
    {
        var newguid = Guid.NewGuid();

        newguid.HasValue();
    }
Tasso Mello
  • 347
  • 2
  • 5
  • 1
    I wouldn't go this route. Your method name is a true/false question and therefor it should return a boolean. I doubt there are many codebases where you'd want to throw an exception when checking for a value, especially when a nullable type is perfectly valid to pass in – jhammond Sep 22 '21 at 18:40
  • 1
    OK with the general idea of this approach with two strong objections: Like jhammond has said, don't name it "hasSomething" if it's not meant to return a boolean but instead only meant to raise an Exception. Name it "AssertSomething", C++-style. More genrally, don't give a function the same name as a native function of the System. C# already defines its own HasValue for nullable types! – jeancallisti Sep 29 '22 at 16:45