12

I have a little confusion with the following things:

  1. Null
  2. DBNull.Value
  3. ""

When I use Conditional Statements OR while assigning values, I am a little bit confused with these things. Sometimes it throws error and some times it works. I want to know when I want to use the above things. Are they specific with datatypes? I need your valuable suggestions please.

3 Answers 3

15

null is one of two things:

  • a reference that doesn't actually point to an object - just a "nothing" indicator (essentially, it is the value 0 as a reference)
  • a Nullable<T> struct, which does not currently have a value (the HasValue property will also return false)

DBNull is specific to some parts of ADO.NET to represent null in the database. I have yet to think of a good reason why they didn't just use regular null here.

"" is a string literal with length zero - a perfectly valid, but empty, string. The significance of this is that between null string and a "" string, instance methods like value.Trim() will behave differently; null.Trim() will throw an exception; "".Trim() is just "". In general, using string.IsNullOrEmpty(value) as a test makes this distinction go away.

Sign up to request clarification or add additional context in comments.

4 Comments

+1: I don't have any first hand knowledge of why for DBNull, but I've found it useful personally in distinguishing between a null object in memory (which was bad for me) and a proper null value in the database (which I needed). I'm sure they could have built it to use null for both, but I found the distinction helpful.
@Joel fair enough; I've never had though, sadly. It is just one more thing to remember... param.Value = value ?? DBNull.Value; etc
@MarcGravell one case when it is useful, is in the return value of SqlCommand.ExecuteScalar. null is returned when the result set had zero rows. DBNull.Value is returned when there was at least one row, but the first column had a database NULL in it.
@MoeSisko yes, that is certainly one case where there is a difference, but that doesn't seem adequate justification for the amount of confusion this has added - frankly, just about any other API would have been preferable to having this null duality
12
  • null is internal to the language and simply means that the object reference currently doesn't refer to an actual object. Basically, a key difference between value types (int, bool, etc.) and reference types (Object, any class, etc.) is that value types are a concrete value in memory and reference types are a pointer to a representation of the object in memory. That pointer can sometimes point to, well, nothing. In which case it's null. It basically means "No C# object exists here."

  • DBNull.Value is slightly different for the purpose of working with databases. A database column can contain a null value. However, when that data is selected into an object (such as a DataTable) in code, there is an object there to reference as far as the code is concerned. However, that object contains a representation of a null value from the database. Thus, DBNull.Value exists to represent that distinction. It basically means "There is a C# object here which grabbed a value from a database, but that value is null."

  • "" (or string.Empty if you want to use a built-in constant for that) is a valid value. It exists, it's not null, it's in every way a string. It just doesn't have any characters in it. A useful tool for checking this is string.IsNullOrEmpty() which checks for either null or the empty string.

2 Comments

"so you don't have to instantiate a new string in memory to hold nothing" - the interning makes this pretty much moot. I wouldn't stress over the difference between "" and string.Empty
@Marc Gravell: True, it's pretty well optimized at this point. Old pet peeves die hard, I guess.
1

null applies to C# null values.

System.DBNull.value applies to database specific NULL values.

When you use ExecuteScalar() it gives null if column not found or returned, but it column found and value is null there then it returns DBnull, means in a column if value is null, if will return DBnull not null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.