Mads Torgersen has publicly said that a null-propagating operator is under consideration for the next version of C# (but also emphasised that this doesn't mean it will be there). This would allow code like:
var value = someValue?.Method()?.AnotherMethod();
where the ?.
returns null
if the operand (on the left) is null
, else will evaluate the right hand side. I suspect that would get you a lot of the way here, especially if combined with (say) extension methods; for example:
DateTime? dtStartDate = strStartDate?.MyParse();
where:
static DateTime MyParse(this string value) {
return DateTime.ParseExact(value, "dd.MM.yyyy",
System.Globalization.CultureInfo.InvariantCulture
);
However! You could do the same thing right now just using extension methods:
DateTime? dtStartDate = strStartDate.MyParse();
static DateTime? MyParse(this string value) {
if(value == null) return null;
return DateTime.ParseExact(value, "dd.MM.yyyy",
System.Globalization.CultureInfo.InvariantCulture
);