Finding null value in Dataset - DataRow.IsNull method vs ==DbNull.Value - c#

Jarrod picture Jarrod · Apr 8, 2011 · Viewed 27.5k times · Source

What are the benefits of using the c# method DataRow.IsNull to determine a null value over checking if the row equals DbNull.value?

if(ds.Tables[0].Rows[0].IsNull("ROWNAME")) {do stuff}

vs

if(ds.Tables[0].Rows[0]["ROWNAME"] == DbNull.value) {do stuff}

Answer

Adam Robinson picture Adam Robinson · Apr 11, 2011

There is no real practical benefit. Use whichever one seems more readable to you.

As to the particular differences between them, the basic answer is that IsNull queries the null state for a particular record within a column. Using == DBNull.Value actually retrieves the value and does substitution in the case that it's actually null. In other words, IsNull checks the state without actually retrieving the value, and thus is slightly faster (in theory, at least).

It's theoretically possible for a column to return something other than DBNull.Value for a null value if you were to use a custom storage type, but this is never done (in my experience). If this were the case, IsNull would handle the case where the storage type used something other than DBNull.Value, but, again, I've never seen this done.