0

Output from VS Immediate Window

default(Guid?)
=> null
(Guid?)null == default
=> true
(Guid?)null ?? default
=> {00000000-0000-0000-0000-000000000000}

I expected the last to be null and not Guid.Empty. So why is it this way?

  • `(Guid?)null` is null, so you get the right part of the `??`. `default(Guid)` is `Guid.Empty`. – GSerg Feb 23 '21 at 12:46
  • @GSerg but say i have a variable: `Guid? toCheck = null` do some fancy stuff, maybe set toCheck or not and later on i check `var result = toCheck ?? default;` i would not expect result to be Guid.Empty – Michael Prüfer Feb 23 '21 at 12:48
  • With `var g1 = (Guid?)null; var g2 = g1 ?? default;`, you can observe that the inferred type is `Guid?` for `g1` and `Guid` for `g2`. By changing the latter to `var g2 = g1 ?? default(Guid?);`, you can make `g2` also `Guid?`. – GSerg Feb 23 '21 at 12:53
  • @GSerg Yes, thank you – Michael Prüfer Feb 23 '21 at 12:58

0 Answers0