Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion with NULLs in C#

Tags:

c#

.net

.net-2.0

I am always confused with the different ways of expressing nulls. There is the null reference type (aka "null"). Then I've seen that throughout my application, developers have used MinValue to represent nulls. Ex: Double.MinValue or DateTime.MinValue except for a String for which they use "null"

Then there is System.DBNull (and System.DBNull.Value - not sure what to use when). To add to the confusion, there are also System.Nullable and System.Nullable<T> namespaces.

Can someone help me clear this null confusion?

Thanks

like image 866
DotnetDude Avatar asked Apr 03 '09 14:04

DotnetDude


4 Answers

Sure.

System.DBNull is a class that was (and still is) used by ADO.NET to represent a null value in a database.

null is actually a null reference, and in your application code any reference type should use null as its, well, null value.

The usage of MinValue for various primitive types (which, since they are value types cannot be assigned null) dates back to the dark days before C# 2.0, which introduced generics. Now the preferred method for representing a nullable primitive type is to use the Nullable<T> generic type, usually represented in shorthand with a question mark after the primitive type. For example, I could declare a nullable int variable named foo in C# as:

Nullable<int> foo;

or

int? foo;

Both are identical.

like image 118
Adam Robinson Avatar answered Oct 16 '22 01:10

Adam Robinson


  • null is only valid for reference types: types that are a class rather than a structure.
  • .Net also has value types: int, double, DateTime, etc. Value types cannot be null, you so you normally compare those to their default value, which is very often type.MinValue, but might be something (consider Boolean, for example).
  • Nullable<T> is for when you have a value type that might genuinely be null. The default value (maybe MinValue) is also valid and you need to distinguish it from when the variable has not been assigned yet. In C#, you can use a ? with a value type as a short hand notation (for example: int?), but you're still creating the same Nullable<T>.
  • DBNull specifically refers to NULL values from a database. It's not the same thing as using null elsewhere in the language: it's only for talking to a database so you can know when a query returned a null value.
  • When using generics, you'll also see the default(T) construct used, where T is a type parameter. This allows you to set a type's default value without knowing whether that type is a reference type or a value type, much less what a specific value type's default value might be.
like image 31
Joel Coehoorn Avatar answered Oct 16 '22 02:10

Joel Coehoorn


A Null value represents any reference type object that has not had its memory allocated yet.

The MinValue does not represent a Null, rather it is a property that is often used to represent the smallest possible constant value that a given value type can have.

The DBNull.Value class is a mapping of the Nulls returned/passed to a database.

The Nullable generic type enables you to assign Null values to a Value-type.

like image 42
Cerebrus Avatar answered Oct 16 '22 02:10

Cerebrus


  • null for reference types in the actual null value
  • Nullable, added in .NET 2.0 is a way of expressing nulls for value types, which by definition can not be null.
  • Same with DateTime.MinValue - DateTime is a value type, and can not be null, so you can have a convention that a well known value, like DateTime.MinValue is treated as if it was null. It also has other usages.
like image 43
Krzysztof Kozmic Avatar answered Oct 16 '22 03:10

Krzysztof Kozmic