1

Our application requires int? variables. I'm often checking both to make sure not null and not 0 and it does get lengthy.

Is there an out of the box version of String.IsNullOrWhiteSpace() or String.IsNullOrEmpty() for int?

Maybe this would require an extension method?

If there even was or when someone makes one, would something like this considered bad practice?

markokstate
  • 923
  • 2
  • 14
  • 28

1 Answers1

1

I don't think so but it's easy to write your own:

[Pure]
public static bool IsNullOrDefault<T>(this T? pValue)
where T : struct {
   return pValue == null || pValue.Value.Equals(default(T));

   // or as suggested in comments (tested)
   return pValue == null || EqualityComparer<T?>.Default.Equals(pValue, default(T));
}
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80