Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(any == System.DBNull.Value) vs (any is System.DBNull)

Tags:

.net

null

dbnull

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!

like image 712
Ricardo Villamil Avatar asked Sep 19 '08 21:09

Ricardo Villamil


3 Answers

I tend to use

if (DBNull.Value.Equals(value)) {
    //
}

or

if (Convert.IsDBNull(value)) {
    //
}
like image 74
Billy Jo Avatar answered Nov 17 '22 00:11

Billy Jo


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.

like image 21
Omer van Kloeten Avatar answered Nov 17 '22 00:11

Omer van Kloeten


if (any == System.DBNull.Value) ...

I prefer that one, simply because I read that as comparing values, not types.

like image 26
MagicKat Avatar answered Nov 16 '22 23:11

MagicKat