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

Does anyone have a preference on how to check if DBNull ? I found that these two statements give me the results I want, but just wondering if there is a preference?

if (any is System.DBNull)

same as:

if (any == System.DBNull.Value)

Thank!

+10
null dbnull
Sep 19 '08 at 21:13
source share
6 answers
 if (any == System.DBNull.Value) ... 

I prefer this, simply because I read it as a comparison of values, not types.

+4
Sep 19 '08 at 21:14
source share

I try to use

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

or

 if (Convert.IsDBNull(value)) { // } 
+11
Sep 19 '08 at 21:20
source share

is does not use reflection, as Kevlar623 says. It maps the isinst operation to IL. At this level, a performance comparison is simply stupid, unless you are working on a missile control system.

I am using value is DBNull . It just sounds right, and as a paranoid developer, I cannot assume that the only value that has ever existed is DBNull.Value . Mistakes occur.

+5
Sep 20 '08 at 22:41
source share

if you are in C # you should use == ; is uses reflection, which is more expensive to compute, especially since there is only one instance of System.DBNull .

0
Sep 19 '08 at 21:16
source share

I like "is System.DBNull" more because I hate the idea of ​​comparing something with NULL and being true. Many other syntaxes (what the hell is this plural?) Would have anything == NULL, return NULL.

I understand that there DBNull.Value for some reason. I know. I list my PREFERENCE :)

-one
Sep 19 '08 at 21:17
source share

This is a good example of a form following a function. Whatever is more effective, this is the way to go. The way it looks, reads, or the bad names that he gives you does not matter. Use the language effectively, do not format the language into a new one.

-2
Dec 23 '08 at 18:46
source share



All Articles