Broow Broow - 4 months ago 27
C# Question

Please explain to me the code provided

As for newbie programmer like me please explain the code below especially the

  1. Nullable<>

  2. !string.IsNnullOrEmpty

    Nullable<DateTime> startDate = !string.IsNullOrEmpty(l_hdr.StartDate.ToString()) ? new Nullable<DateTime>(Convert.ToDateTime(l_hdr.StartDate)) : null;

And rest of the is


The main thing to understand here is the difference between value types and reference types. All types store a value of some kind, but reference types always store a special kind of value, called a reference. References are used to refer to more complicated objects that live somewhere else in memory. There is also a special reference, called null, which means the variable doesn't refer to anything at all right now. Value types, on the other hand, hold values. Values are always there. They can never just be null. Every value type has a default value that is used for variables that don't have anything else assigned.

Armed with that knowledge, we can begin to understand the Nullable<T> type. Nullable<T> exists to allow you to use a value type when you need to know difference between a true null (no value yet) and the default value for that type. Nullable<T> is still a value type, but it has special handling in the compiler so you can use the null keyword with it in certain ways.

Now we can begin to look at the code. Unfortunately, this code sample doesn't make much sense. Let's break it apart to see why. I'll start with this expression:


If the l_hdr object here is null, this part of the expression throws an exception as soon as you try to check it's StartDate property, and nothing else matters. Therefore we can assume that l_hdr is not null.

But what about that StartDate property? This looks a lot like a DateTime value. But if that's true, the conditional expression makes no sense because DateTime values are value types. Value types are never null, and thus there is no possible way for ToString() called on a DateTime value to ever return an empty string or null, meaning the following expression will always be true:


Again: we know l_hdr is not null, and thus l_hdr.StartDate.ToString() must produce a string of some length. The string is not null or empty, and that is itself negated... thus always true.


StartDate might also be a Nullable<DateTime> value, instead of a simple DateTime value. But this too is ridiculous, when you look at the whole line of code:

Nullable<DateTime> startDate = !string.IsNullOrEmpty(l_hdr.StartDate.ToString()) ? new Nullable<DateTime>(Convert.ToDateTime(l_hdr.StartDate)) : null;

All of that code exists to assign l_hdr.StartDate to a new Nullable<DateTime> variable. But if l_hdr.StartDate is already a Nullable<DateTime> value, then that entire complicated line of code simplifies down to just this:

var startDate = l_hdr.StartDate;

Really. All that code really doesn't do anything at all.

The one scenario I've come up with where this kind of sort of almost makes sense is if someone decided to store DateTime values in a varchar column in a database (which you should never do!). In that situation, the l_hdr object could represent a record from the table, which could includes these string DateTime values, such that the actual defined type of the StartDate property is just Object, to match how DataReader or DataTable hold column values.

At this point, possible values in the StartDate property are a DateTime string, an empty string (which effectively means null), and DBNull.Value. Calling .ToString() on those values is a one way to take care of DBNull.Value situations, and this also explains the need to use Convert.ToDateTime(). But it's not exactly how I'd want to handle this.

I suppose any situation that involves an Object or dynamic property where you expect something that might be a string, null, or DateTime value could warrant code that looks something like this, but it's not a situation where I'd want to find myself if I could avoid it.