11

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!

icebat
  • 4,696
  • 4
  • 22
  • 36
Ricardo Villamil
  • 5,031
  • 2
  • 30
  • 26

6 Answers6

11

I tend to use

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

or

if (Convert.IsDBNull(value)) {
    //
}
Billy Jo
  • 1,326
  • 19
  • 32
5

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.

Omer van Kloeten
  • 11,800
  • 9
  • 42
  • 53
  • I'd worry more about other bugs than whether a sealed system class with the sole value of `Value` is somehow going to sprout a second value. However I agree with you, not because I'm paranoid, but because the test against DBNull.Value to my mind suggests the possibility that there **could be** some other DBNull. I *know* there is not, but why write code that sounds like it is referring to a specific DBNull, when indeed there is only one? Always seemed silly to me. – ToolmakerSteve Aug 20 '14 at 22:24
4
if (any == System.DBNull.Value) ...

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

MagicKat
  • 9,695
  • 6
  • 32
  • 43
  • Won't that give you an Exception if the any variable is not of type object? Eg string? Comparing a string with DBNull will fail. – David Klempfner Jul 08 '20 at 02:21
0

if you're in c#, you should use ==; is uses reflection which is more expensive to compute, especially since there's only ever one instance of System.DBNull.

Brad
  • 15,361
  • 6
  • 36
  • 57
Kevlar
  • 8,804
  • 9
  • 55
  • 81
  • -1 for the incorrect statement `is uses reflection`. There is no performance problem using `is`. (To be more precise, what is usually referred to as "using reflection" is to use the methods in System.Reflection. Which indeed are slow. So to hand-wavingly refer to the use of `is` as "using reflection" is at best misleading. So misleading that this answerer ASSUMES that the result must be slow. Did you actually **test** this assumption? No.) – ToolmakerSteve Aug 20 '14 at 22:01
-1

I like the "is System.DBNull" more because I hate the idea of comparing something to NULL and having it be true. Many other syntaxes (what the hell is the plural of that?) would have anything==NULL return NULL.

I understand that there's DBNull.Value for a reason. I know. I'm listing my PREFERENCE :)

nathaniel
  • 1,135
  • 2
  • 8
  • 20
  • Huh? This is a .Net question. `x == null` does NOT return `null`. Doing a google search, I haven't found any other language in which it does. Perhaps you are thinking of float operations where `x == undefined` returns `undefined`. But that is different than `null`. `null` has a specific meaning, and can indeed be compared to. For more details about what such a comparison means, and whether it is a good idea or not, see http://stackoverflow.com/questions/3507383/equalsitem-null-or-item-null (And in C++, from which much of c#s syntax comes, it is often NECESSARY to compare to `null`.) – ToolmakerSteve Aug 20 '14 at 22:12
  • Hi @ToolmakerSteve! Boy this is an old question that you're dropping in on. It's related to data, and in SQL the comparison (somevar = null) returns null. – nathaniel Oct 15 '14 at 19:19
-2

This is a good example of form follows function. Whichever one executes more efficiently is the way to go. What it looks like, reads like, or bad names it calls you is irrelevant. Use the language efficiently, don't mold the language into a new one.

spoulson
  • 21,335
  • 15
  • 77
  • 102