Does any one have a preference on how to check if a value is DBNull
? I've found these two statements give me the results I want, but just wondering if there's a preference?
if (any is System.DBNull)
same as:
if (any == System.DBNull.Value)
Thanks!
I tend to use
if (DBNull.Value.Equals(value)) {
//
}
or
if (Convert.IsDBNull(value)) {
//
}
is
does not use reflection as Kevlar623 says. It maps to the isinst
operation in IL. On that level, comparing performance is downright silly, unless you're working on a missile guidance system.
I use value is DBNull
. It just sounds right and as a paranoid developer, I can't trust that the only value ever in existence is DBNull.Value
. Bugs happen.
if (any == System.DBNull.Value) ...
I prefer that one, simply because I read that as comparing values, not types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With