Check if Nullable Guid is empty in c#
Asked Answered
T

7

76

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)
Toxinantitoxin answered 17/7, 2013 at 7:49 Comment(4)
There's an answer to that question that addresses a nullable guid - a.k.a. Nullable<Guid> or Guid?Suzisuzie
I think this link would be helpful for you #676578Held
helpful indeed, thanks!Toxinantitoxin
NOTE - this question is specifc. about Nullabe Guid's... For how to check if a regular Guid is empty: #9838102Beeswing
D
149

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}

Defazio answered 17/7, 2013 at 7:55 Comment(4)
to confirm: Guid.Empty.ToString() == "00000000-0000-0000-0000-000000000000"Gery
Guid is never null, but it can be Guid.EmptyLandgraviate
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 ^^Defazio
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.Imperceptive
W
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> ;)

Welch answered 17/7, 2013 at 7:52 Comment(4)
this is only the correct answer when using a Nullable Guid. I guess most people use Guid? instead of GuidBeeswing
Not sufficient. A null Guid is not the same as an empty Guid (i.e. Guid.Empty). So you not only need to check Guid?.HasValue, you also have to check whether Guid?.Value == Guid.Empty.Kerb
As @Kerb has said this answer needs to be updated to also check for Guid.EmptyWinfield
This incorrectly answers the question.Panamerican
H
20

Note that HasValue will return true for an empty Guid.

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

Haywood answered 3/9, 2013 at 15:56 Comment(0)
M
4

Check Nullable<T>.HasValue

if(!SomeProperty.HasValue ||SomeProperty.Value == Guid.Empty)
{
 //not valid GUID
}
else
{
 //Valid GUID
}
Manta answered 17/7, 2013 at 7:56 Comment(1)
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.Extremist
A
4

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

Already answered 31/5, 2020 at 10:48 Comment(0)
Y
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

Yasmineyasu answered 17/7, 2013 at 7:53 Comment(1)
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.Extremist
G
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();
    }
Grane answered 26/5, 2021 at 10:51 Comment(2)
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 inInhaul
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!Extremist

© 2022 - 2024 — McMap. All rights reserved.