2

Possible Duplicate:
What is the point of DBNull?

Since the beginning of my adventure with .NET I have always asked myself one question:

why do we need the DBNull type? Is a simple null reference not enough?

MSDN says that DBNull "Represents a nonexistent value". From a logical point of view - this one sentence explains why a null reference cannot be used - because it is a null reference and not a lack of value. But is it enough to introduce a type that causes a lot of trouble?

BTW: If anyone has something to say in defense of the DBNull I would really appreciate it.

Community
  • 1
  • 1
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59

4 Answers4

8

DBNull has existed since the earliest versions of .Net framework.

Nullable value types (Nullable<T>) has only existed since version 2 of the framework.

Since, before version 2, it was possible to receive, say, a null int value back from the database, how would you propose it be represented? Then, for consistency, the same was used to represent all DB nulls, whether they translated to a value or reference type.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
2

DBNull.Value represents 'NULL' in the database; it is not the same as 'null'.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
1

We need it so we can detect if a value returned from a database is null or not. Typically, Convert.IsDBNull is used to perform the check.

The DBNull type is not the same as the programming value null

IndigoDelta
  • 1,481
  • 9
  • 11
0

null in c# is something different than NULL in the SQL context, the SQL NULL in C# is exposed not as string but as DbNull.Value which makes sense, if it was not like that how would you check for db null? comparing a string "NULL" would not have been the same...

Davide Piras
  • 43,984
  • 10
  • 98
  • 147