41

I have an object of type Employee which has a Guid property. I know if I want to set to null I must to define my type property as nullable Nullable<Guid> prop or Guid? prop.

But in my case I'm not able to change the type of the prop, so it will remains as Guid type and my colleague and I we don't want to use the Guid.Empty.

Is there a way to set my property as null or string.empty in order to restablish the field in the database as null.

I have a mechanism to transform from string.empty to null but I will change many things if the would change to accept a empty guid to null.

Any help please!

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • 1
    A `Guid` is a `struct`, those can't be null. You need to use the nullable type. Either using `Guid?` or `Nullable`. There is no other way. – Jeroen Vannevel Dec 10 '13 at 17:24
  • 1
    When I see something like "I know I should do this but I can't", stop right there. Yes you can and you should. Why would you cause yourself a world of hurt by hacking stuff? – fejesjoco Dec 10 '13 at 17:27

8 Answers8

86

Is there a way to set my property as null or string.empty in order to restablish the field in the database as null.

No. Because it's non-nullable. If you want it to be nullable, you have to use Nullable<Guid> - if you didn't, there'd be no point in having Nullable<T> to start with. You've got a fundamental issue here - which you actually know, given your first paragraph. You've said, "I know if I want to achieve A, I must do B - but I want to achieve A without doing B." That's impossible by definition.

The closest you can get is to use one specific GUID to stand in for a null value - Guid.Empty (also available as default(Guid) where appropriate, e.g. for the default value of an optional parameter) being the obvious candidate, but one you've rejected for unspecified reasons.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    And to elaborate a little, it's non-nullable because it's a struct and thus a value type. Value types cannot be null. `Nullable` can wrap a value type in a different value type that can represent a null value. If you were to make it `Nullable` or `Guid?` for short, you can represent the DB side as a `UNIQUEIDENTIFIER NULL` – Haney Dec 10 '13 at 17:25
  • 1
    @DavidHaney: No, you've misunderstood `Nullable` - that's a value type too, but it has a value representing the absence of a wrapped value. `Nullable` would be a lot less pleasant if it were genuinely a reference type. – Jon Skeet Dec 10 '13 at 17:26
  • Awesome, I didn't realize that. I'll disassemble and stare at it now. Thanks for the free lesson. :) On a side note, why would it be a lot less pleasant as a reference type? – Haney Dec 10 '13 at 17:27
  • @DavidHaney: Because then you'd have to create an instance (on the heap) for every value, rather than the value being "inline" - that would lead to more GC pressure. – Jon Skeet Dec 10 '13 at 17:29
  • 2
    @JonSkeet I cannot use Guid.Empty because I want it to be a default value for method parameter. As compiler points out "Default parameter value for X must be a compile-time constant"! – Kishan Vaishnav May 16 '19 at 12:38
  • 3
    @KishanVaishnav: You can use `default(Guid)` for that. It's equivalent to `Guid.Empty`. – Jon Skeet May 16 '19 at 12:40
  • @JonSkeet Thanks. If you update the answer with this would be helpful for some people. – Kishan Vaishnav May 17 '19 at 03:45
  • Guid.Empty is the best solution for me Tanx – R.Akhlaghi Dec 07 '22 at 11:22
36
Guid? myGuidVar = (Guid?)null;

It could be. Unnecessary casting not required.

Guid? myGuidVar = null;
Ajay
  • 6,418
  • 18
  • 79
  • 130
A.S
  • 369
  • 3
  • 3
  • Interesting comment that if you have a nullable Guid, it should be concreted as nullable everywhere (I mean even as parameter inside a method). – Gonzo345 Jun 15 '17 at 10:20
  • This helped me where db returns a strring, model expects Nullable GUID UserRoleID = String.IsNullOrEmpty(userrole.RoleId) ? (Guid?)null : new Guid(userrole.RoleId); thanks – Doug Jan 08 '18 at 13:08
  • 1
    How did this get 18 upvotes when the question clearly states it's not a valid answer? Was this merged from another question? – nvoigt Nov 30 '18 at 17:05
24

Since "Guid" is not nullable, use "Guid.Empty" as default value.

macrobbsen
  • 278
  • 1
  • 6
9

Choose your poison - if you can't change the type of the property to be nullable then you're going to have to use a "magic" value to represent NULL. Guid.Empty seems as good as any unless you have some specific reason for not wanting to use it. A second choice would be Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff") but that's a lot uglier IMHO.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
7

You can use typeof(Guid), "00000000-0000-0000-0000-000000000000" for DefaultValue of the property.

Liam
  • 27,717
  • 28
  • 128
  • 190
Juke Exch
  • 121
  • 1
  • 3
3

you can make guid variable to accept null first using ? operator then you use Guid.Empty or typecast it to null using (Guid?)null;

eg:

 Guid? id = Guid.Empty;

or

 Guid? id =  (Guid?)null;
Liam
  • 27,717
  • 28
  • 128
  • 190
Ranjith
  • 59
  • 1
  • 6
0

I think this is the correct way:

Guid filed = Guid.Empty;

Ángel Ibáñez
  • 329
  • 1
  • 6
0

extrac Guid values from database functions:

    #region GUID

    public static Guid GGuid(SqlDataReader reader, string field)
    {
        try
        {
            return reader[field] == DBNull.Value ? Guid.Empty : (Guid)reader[field];
        }
        catch { return Guid.Empty; }
    }

    public static Guid GGuid(SqlDataReader reader, int ordinal = 0)
    {
        try
        {
            return reader[ordinal] == DBNull.Value ? Guid.Empty : (Guid)reader[ordinal];
        }
        catch { return Guid.Empty; }
    }

    public static Guid? NGuid(SqlDataReader reader, string field)
    {
        try
        {
            if (reader[field] == DBNull.Value) return (Guid?)null; else return (Guid)reader[field];
        }
        catch { return (Guid?)null; }
    }

    public static Guid? NGuid(SqlDataReader reader, int ordinal = 0)
    {
        try
        {
            if (reader[ordinal] == DBNull.Value) return (Guid?)null; else return (Guid)reader[ordinal];
        }
        catch { return (Guid?)null; }
    }

    #endregion
Ángel Ibáñez
  • 329
  • 1
  • 6