115

This keeps me wondering why Guid in .NET does not have IsNullOrEmpty() method (where empty means all zeros)

I need this at several places in my ASP.NET MVC code when writing the REST API.

Or am I missing something because nobody on the Internet has asked for the same?

tereško
  • 58,060
  • 25
  • 98
  • 150
Gautam Jain
  • 6,789
  • 10
  • 48
  • 67
  • To start off http://stackoverflow.com/questions/2344213/is-guid-considered-a-value-type-or-reference-type but for your needs there is `Guid.Empty` – V4Vendetta Mar 23 '12 at 10:31

7 Answers7

260

Guid is a value type, so a variable of type Guid can't be null to start with. If you want to know if it's the same as the empty guid, you can just use:

if (guid == Guid.Empty)
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • What about cases where Entity Framework defines Guid variables as Guid? (nullable)? – Gautam Jain Mar 23 '12 at 10:39
  • 7
    @goths: Then you could use `if (nullableGuid == null || nullableGuid == Guid.Empty)`... or create your own extension method if you really want. Presumably it comes up rarely enough that it's not worth it for most people. – Jon Skeet Mar 23 '12 at 10:42
  • 3
    @goths You can make a general extension method for all value types. For example: `public static bool IsNullOrDefault(this T? self) where T : struct { return !self.HasValue || self.Value.Equals(default(T)); }` – Jeppe Stig Nielsen Sep 07 '13 at 22:27
33

For one thing, Guid is not nullable. You could check:

myGuid == default(Guid)

which is equivalent to:

myGuid == Guid.Empty
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
12

Here is a simple extension method for a nullable Guid.

/// <summary>
/// Determines if a nullable Guid (Guid?) is null or Guid.Empty
/// </summary>
public static bool IsNullOrEmpty(this Guid? guid)
{
  return (!guid.HasValue || guid.Value == Guid.Empty);
}

UPDATE

If you really wanted to use this everywhere you could write another extension method for a regular Guid. It can never be null, so some people won't like this... but it serves the purpose you are looking for and you don't have to know whether you are working with Guid? or Guid (nice for re-factoring etc.).

/// <summary>
/// Determines if Guid is Guid.Empty
/// </summary>
public static bool IsNullOrEmpty(this Guid guid)
{
  return (guid == Guid.Empty);
}

Now you could use someGuid.IsNullOrEmpty(); in all cases, whether you are using Guid or Guid?.

Like I said, some people will complain about the naming because IsNullOrEmpty() implies that the value could be null (when it can't). If you really wanted to, come up with a different name for the extensions like IsNothing() or IsInsignificant() or whatever :)

Justin
  • 1,310
  • 3
  • 18
  • 29
2

As others have pointed out, the premise of the question isn't all there. C# Guid is not nullable. However, Guid? is. A clean way of checking if a Guid? is null or Guid.Empty is by check if the result of GetValueOrDefault() is Guid.Empty. E.g.,

Guid? id;

// some logic sets id

if (Guid.Empty.Equals(guid.GetValueOrDefault()))
{
    // Do something
}
2

You can make an extension method to Guid to add IsEmpty functionality:

public static class GuidEx
{
    public static bool IsEmpty(this Guid guid)
    {
        return guid == Guid.Empty;
    }
}

public class MyClass
{
    public void Foo()
    {
        Guid g;
        bool b;

        b = g.IsEmpty(); // true

        g = Guid.NewGuid();

        b = g.IsEmpty; // false

        b = Guid.Empty.IsEmpty(); // true
    }
}
SimpleVar
  • 14,044
  • 4
  • 38
  • 60
0

Here is a generic extension class for nullable struct types:

public static class NullableExtensions
{
    public static bool IsNullOrDefault<T>(this T? self) where T : struct
    {
        return !self.HasValue || self.Value.Equals(default(T));
    }
}
Jamaxack
  • 2,400
  • 2
  • 24
  • 42
-2

You know I see this statements like this one all the time

Guid is a value type, so a variable of type Guid can't be null to start with

but it is just NOT TRUE.

Agreed you can not programmatic set a Guid to null, but when some SQL pulls in a UniqueIdentifier and maps it to a Guid, and if that value is null in the db, the value comes up as null in the C#.

Paul Gorbas
  • 1,694
  • 20
  • 16
  • If the field is nullable in the database then it would be more correct to specify that the type is nullable (Guid?) – cdimitroulas Sep 29 '21 at 07:58