Search code examples
c#conditional-statementsnull-coalescing-operator

Null coalescing operator with the variable itself


Is there any way of reduce the null coalescing operator expression when the variable we check is the one we will assign if it's not null?

Example:

DateTime? date1 = DateTime.Parse("11/05/1990");
DateTime? date2 = DateTime.Now;
date1 = date1 ?? date2;

For instance, something like that:

date1 = ?? date2;

I know that's not a big deal but I'm curious.


Solution

  • No, this does not exist in C#.

    You can find the list of operators in https://msdn.microsoft.com/nl-nl/library/6a71f45d.aspx.

    But isn't date1 = date1 ?? date2 short enough already ?