I'm attempting to convert an
if (value is bool)
boolValue = (bool) value;
else if (value is bool? && ((bool?)value).HasValue)
boolValue = ((bool?) value).Value;
throw new ArgumentException("Value must be a boolean type");
value is bool?
value is bool
Objects based on nullable types are only boxed if the object is non-null. If HasValue is false, the object reference is assigned to null instead of boxing ... Boxing a non-null nullable value type boxes the value type itself, not the System.Nullable that wraps the value type.
And, per the documentation for
An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.
So, using both of these you can deduce (see fiddle) that in the null case:
bool? x = null; object obj = x; // assigns obj = null obj is bool? // false, obj is null obj is bool // false, obj is null
And in the non-null case:
bool? x = true; object obj = x; // obj is boxed bool (true) obj is bool? // true, obj unboxes to bool? obj is bool // true, obj unboxes to bool
So ReSharper is correct: your first branch will evaluate as
false (whether the object was assigned from a
bool? is not relevant or even known). The second branch will always be
false in this case.