1
 protected static SqlParameter CreateParameter(string name, object value, bool skipEmpty, bool isOutput =false)
 {
    if (skipEmpty && value is string && string.IsNullOrEmpty((string)value))
        return null;
    //1
    if (skipEmpty && value is int? && value == null)
      return null;
    //2
    if (skipEmpty && value is Guid? && value == null)
      return null;
    //....................
}

The resharper says that 1 and 2 clauses are always false. But why?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Alexandre
  • 13,030
  • 35
  • 114
  • 173

4 Answers4

6

If value is null then it is impossible to infer a type more complex than object from it, therefore it can never be int? or Guid?

spender
  • 117,338
  • 33
  • 229
  • 351
3

if value is an object, and is null, what type was it? It doesn't have a type.

You've got a variable of type object, which doesn't contain anything. There's no way to know what someone else may have planned to place in that variable.


Or as the answer to C# get type of null object put it:

That's like asking what kind of cake would have been in an empty box with no label.

Community
  • 1
  • 1
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
2

An is expression evaluates to true if the provided expression is non-null. Therefore, the expression (value is int? && value == null) always equals false

vvk
  • 833
  • 5
  • 16
1

This should work:

if (skipEmpty && ((value is string && string.IsNullOrEmpty((string)value)) || value == null)) return null;
lesderid
  • 3,388
  • 8
  • 39
  • 65