Confusion with NULL in C #

Different ways of expressing zeros always confuse me. There is a null reference type (aka "null"). Then I saw that throughout my application, developers used MinValue to represent zeros. Example: Double.MinValue or DateTime.MinValue, except for the 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 confusion?

thanks

+7
source share
10 answers

Of course.

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

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

The use of MinValue for various primitive types (which, since they are value types, cannot be null ), refers to the dark days before C # 2.0, into which generics were introduced. Currently, the preferred method for representing a primitive type with a null value is to use the Nullable<T> type type, usually abbreviated with a question mark after the primitive type. For example, I can declare a variable with a null int value named foo in C # as:

 Nullable<int> foo; 

or

 int? foo; 

Both are identical.

+27
source share
  • null is only applicable for reference types: class types, not structure .
  • .Net also has value types: int, double, DateTime, etc. Value types cannot be null, so you usually compare them with their default value, which is often type.MinValue , but it can be something (consider a Boolean boolean, for example).
  • Nullable<T> is when you have a value type that can really be null. The default value (possibly MinValue) is also valid, and you need to distinguish it from when the variable has not yet been assigned. In C # can you use? with a value type in the form of a short musical notation (for example: int? ), but you still create the same Nullable<T> .
  • DBNull specifically refers to NULL values ​​from the database. This is not the same as using zero elsewhere in the language: it is only for talking to the database so that you can know when the query returned a null value.
  • When using generics, you will also see the default(T) construct used, where T is the type parameter. This allows you to set the default value of a type without knowing whether that type is a reference type or a value type, and even more so than what a specific default value can be.
+5
source share
  • null for reference types to actual null
  • Nullable, added in .NET 2.0, is a way of expressing zeros for value types that, by definition, cannot be null.
  • The same with DateTime.MinValue - DateTime is a value type and cannot null, so you can have a convention that a known value, for example DateTime.MinValue, is treated as if it were null. It also has other uses.
+1
source share

A Null value represents any object of a reference type for which its memory has not yet been allocated.

MinValue does not represent 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 zeros returned / passed to the database.

The Nullable generic type allows you to assign Null values ​​to type values.

+1
source share

" MinValue " s were used with type values ​​before types with zero values ​​appeared in C # 2.0. Thus, there is a lot of legacy code that uses the old style of knowledge when the type of value does not matter. DateTime? date = null it much easier to use DateTime? date = null DateTime? date = null than DateTime date = DateTime.MinValue . As for DBNull, this is what is required as an abstraction layer for databases, but you can avoid having to deal with it yourself by using ORMs like NHibernate or some of them - you pretty much from the application. development point will only deal with C # built-in types.

0
source share

MinValue is not null. This is MinValue. It is sometimes used "as null" for people using C # 1.0 who don't have null types. Instead, they should use Nullable, ot DateTime ?.

DBNull is a .NET value that is used to communicate with the database ("DB" null). The concept of a NULL database means something other than a .NET null reference. In database terms, NULL means “unknown” or “missing”.

0
source share

Link types (aka objects) can be set to zero, because reference type variables are just a pointer to the actual instance. Specifying the absence of an instance is easy, as you can directly set the variable to null.

For value types, this is a little more complicated, since a value type variable always contains a value. Therefore, the use of Double.MinValue or DateTime.MinValue was somewhat valid on the days preceding Nullable. Then there was no easy way to express the lack of value in types of value.

Now, with null types, you can say:

 double? d = null; 

And so you can also have value type variables containing null.

System.DBNull is a different story because it is directly related to the expression of the value "NULL" in datasets. This was introduced before the NULL types, which imo replace DBNull.

0
source share

Most types in .NET are reference types, and null is a "not reference" that can be assigned to a variable or field to indicate the absence of an object.

All other types in .NET are value types. There is always a value with a type value, so there is no way to indicate "no value". Therefore, an implementation can define specific special values ​​to indicate this. Often, for this, a field of type of value MinValue (constant) is used. Therefore, using DateTime.MinValue for a date of birth may indicate "unknown" or "not applicable" (for example, for a corporate entity).

DbNUll exists to express an RDBMS null value that is slightly different from .NET. In most cases, this will be translated to zero or similar.

Finally, Nullable<T> is a wrapper for value types to express "unknowns" more directly and is generally better than using MinValue , but was added in .NET 2, so older projects may have started using MinValue before Nullable<T> .

0
source share

the best way for tou to understand these different empty values ​​is to try them and see what each of them uses, but to do something simple, are some guidelines

  • null can only be used for reference types such as collections and custom classes (the string is special)
  • DBNull are the null values ​​that came out of the db query.
  • because we cannot assign a null value to decimal or double, we assign them with the MinValue property (they are value objects)

  • Nullable is a way in which a developer can assign null values ​​to evaluate objects such as decimal, Nullable<int> is same as int?


Hope I helped you understand the differences.

0
source share

The important difference here is that between value types and reference types. Value types represent a directly defined value, whereas reference types indicate a memory cell that should represent a value. When the reference type does not actually point to a memory location with valid content, this reference is Null.

Because value types are direct representations of a value, they cannot be null. However, it may be that the actual value of the value type is unknown. For this case, .Net provides a Nullable construct. However, when such a design is not available, people tend to use special or default values ​​such as MinValue.

When communicating with databases, many of the things we expect to be value types can actually be Null, as this is how the database processes unknown values. This can also be solved with Nullable types, but they are not always available. This is why DBNull exists to deal with the possible null value in the database.

0
source share

All Articles