Steve
Steve

Reputation: 4463

null-conditional operator for date type?

In my model, I have this property

public DateTime? ReleaseDate { get; set; }

Then, in my Blazor razor page, I have the below code which has syntax error

<td>@movie.ReleaseDate.ToString("dd-MMM-yyyy")</td>

The issue is because ReleaseDate is nullable but my question is how do I use null-conditional operator ? to get the system current date if ReleaseDate is null?

Upvotes: 0

Views: 627

Answers (2)

Alsein
Alsein

Reputation: 4775

a ?? b (null-coalescing operator) is a short of a == null ? a : b, which means if a is null, then b is returned, otherwise a.

This operator requires that a is of type T? (both Nullable<T> (aka. nullable value types) and nullable reference types are accepted) and b is of type T or T?. If b is of type T?, the return type of this operator is also T?, which means ?? could be chained.

a ?? b ?? c ?? d means that whichever is not null first is returned.

(movie.ReleaseDate ?? DateTime.Now).ToString("dd-MMM-yyyy")

Upvotes: 3

Caius Jard
Caius Jard

Reputation: 74605

Your code has a syntax error because a nullable DateTime? doesn't have a ToString() overload that takes a string format parameter, so you need to get the .Value out of the nullable to turn the nullable into a normal DateTime before you can format it

To answer your specific question about null conditional, this would then something like:

(movie.ReleaseDate.HasValue ? movie.ReleaseDate.Value.ToString("dd-MMM-yyyy") : "Not Yet"

Alsein's form using null coalesce is a lot more readable than using null conditional but his choice to say a movie was released today if it's release date is null doesn't make sense to me. I'd say we need a way of turning it into a date string only if has a value and turning it into some other string if it is null.

Consider:

movie.ReleaseDate?.ToString("dd-MMM-yyyy") ?? "Not Yet"

If release date is null ToString won't be called, and c# will skip straight to the ?? with a null result. ?? will then turn the null into a string of "Not Yet", which is what I presume a null release date might mean..

The ? also works some magic with the nullable, turning it into a DateTime if it does have a value, which is why the earlier "DateTime? doesn't have a ToString(string)" doesn't apply; by the time movie.ReleaseDate? has been evaluated, the result is either a null or a normal DateTime with a ToString that takes a format

?? is thus a good way to give a default value to a chain of calls, any one of which might be null and are protected from throwing null reference by many uses of ? operator:

notnull.MaybeNull?.MayAlsoBeNull?.Something ?? DefaultThing

Also consider whether you should be formatting the date for the user using this fixed way, or whether it would make more sense to use their culture's typical date format and present the date in different formats to different site visitors

See something like this or perhaps this for how to make your code use the user's language declaration to provide culture specific formatting for things like dates and numbers

Upvotes: 4

Related Questions