I am curious what are the pros and cons of using if(some_value is DBNull)
versus if(DBNull.Value.Equals(some_value))
.
Personally i prefer if(some_value is DBNull)
because i find it more readable.
I know Microsoft recommends using if(DBNull.Value.Equals(some_value))
according to https://msdn.microsoft.com/en-us/library/system.dbnull%28v=vs.110%29.aspx.

- 93
- 5
-
4it is not duplicate, this question is different than that one – Ehsan Sajjad Jul 14 '16 at 12:16
3 Answers
I would go for the DBNull.Value.Equals
way.
Why?
Beacuse is
will check the type against equality. It has to look up the left hand type and match that against the right hand type which it also has to look up. After that it can compare the types, most likely by checking for reference equality.
That would be less efficient than just checking reference equality, which DBNull.Value.Equals
does. Since there is just once instance of DBNull.Value
, this check is very accurate and very fast.

- 153,850
- 22
- 249
- 325
-
-
Which would be the same as `Equals` if I am correct @IvanStoev Both check for reference equality. – Patrick Hofman Jul 14 '16 at 12:19
-
Well, not exacly. It will be `ReferenceEquals`, i.e. simple IL instruction. – Ivan Stoev Jul 14 '16 at 12:21
-
Aside from performance are there any differences in the result of the two operations? I understand they are very different, but i could not think of an example when they give different results, since DBNull is sealed. – mobal Jul 15 '16 at 13:26
value is DBNull
actually checks whether value
is an instance of the DBNull
class, while value == DBNull.Value
actually performs a reference comparison between value
and the only instance of the singleton class DBNull
.
The value is DBNull
checks whether value
is an instance of DBNull
, which is only possible if value == DBNull.Value
, since DBNull
is a singleton.
The advantage of using value == DBNull.Value
is that it does a direct reference comparison which will be more efficient than determining the types for the is DBNull
comparison.

- 55,956
- 8
- 91
- 139
some_value is DbNull
: checks the type of some_value against type of DBNull
. This could be used if you could either instantiate an instance of DBNull
OR inherit from it. But no, this class has private constructors and is sealed.
DBNull.Value.Equals(some_value)
: checks value of some_value against the value represented by DBNull.Value
.

- 180,369
- 20
- 160
- 215

- 1,508
- 1
- 14
- 16
-
No pros and cons. Both compare totally different things - type in one case and value in the other. Horses for courses.... – Vijay Gill Jul 14 '16 at 12:15
-
2In general, you are right. But in the DbNull case the result is the same: DbNull.Value is the only possible value of type DbNull. – Hans Kesting Jul 14 '16 at 12:19
-
Can you elaborate how the results is same? Both compare two different things - one compares type and other the value. – Vijay Gill Jul 14 '16 at 12:22
-
@VijayGill It's a Singleton, so there is only one instance of that type `DBNull.Value`. So if the type is the same then the reference will also be the same. And of course if the references are equal then the types are too. – juharr Jul 14 '16 at 12:24
-
@VijayGill Since `DBNull` is a singleton and `DBNull.Value` is the only instance of `DBNull`, `value is DBNull` is only possible if `value == DBNull.Value` and vice versa. – Thorsten Dittmar Jul 14 '16 at 12:25
-
On what basis is it assumed that "some_value" in this question is an instance of DBNull? – Vijay Gill Jul 14 '16 at 12:43
-
@VijayGill That's not the assumption. It is the question. You want to find out whether `some_value` is an instance of `DBNull`. And as `DBNull` is a singleton with its sole instance `DBNull.Value`, `is DBNull` and `== DBNull.Value` both do exactly the same thing in the end, but `== DBNull.Value` does it more efficiently. – Thorsten Dittmar Jul 14 '16 at 12:52
-
@ThorstenDittmar - that I understand, I just got caught-up in **my** assumption that "some_value" could be anything. Thanks for clearing it out. – Vijay Gill Jul 14 '16 at 12:54