How to set null to GUID property

I have an object of type Employee that has a Guid property. I know, if I want to set null, should I define the type property as nullable Nullable<Guid> prop or Guid? prop.

But in my case, I cannot change the type of support, so it will remain the same as the type of Guid, and my colleague, and I do not want to use Guid.Empty.

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

I have a mechanism for converting from string.empty to null, but I would change many things if I changed to accept an empty guid to null.

Any help please!

+14
c # guid nullable
source share
8 answers

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

No, because it is not reset. If you want it to be nullable, you should use Nullable<Guid> - if you didn’t do this, it would be Nullable<T> to start with Nullable<T> . You have a fundamental problem here - which you really know, given your first paragraph. You said, "I know if I want to reach A, I have to do B - but I want to reach A without doing B." This is not possible by definition.

The closest you can get is to use one specific GUID for Guid.Empty zero value - Guid.Empty (also available by default(Guid) where necessary, for example, for the default value of an optional parameter), which is an obvious candidate. but you rejected one for unspecified reasons.

+44
source share
 Guid? myGuidVar = (Guid?)null; 

It can be. Unnecessary casting is not required.

 Guid? myGuidVar = null; 
+18
source share

Choose your poison — if you cannot change the type of the property, which should be null, you will have to use a “magic” value to represent NULL. Guid.Empty looks as good as any other, unless you have a specific reason not to want to use it. The second choice would be Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff") , but it's a lot of ugly IMHO.

+9
source share

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

+8
source share

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

+2
source share

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

eg:

  Guid? id = Guid.Empty; 

or

  Guid? id = (Guid?)null; 
0
source share

extract 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 
0
source share

I think this is the right way:

Guid filed = Guid.Empty;

-one
source share

All Articles