The code below (that will not be production code) will not compile because of an Use of unassigned local variable 'dateTime'
Looking at it I can tell that the variable would never be unassigned at return time. Returning an out parameter in an Any clause is bad form, but why can't it compile?
private static DateTime? ConvertStringToDateByFormat(string date) {
DateTime dateTime;
var acceptableDateFormats = new List<String>{
"d/MM/yyyy",
"d/M/yyyy",
"d/MM/yy",
"d/M/yy",
"d-MM-yyyy",
"d-M-yyyy",
"d-MM-yy",
"d-M-yy",
"d.MM.yyyy",
"d.M.yyyy",
"d.MM.yy",
"d.M.yy",
"d-MMM-yyyy",
"d-MMM-yy",
"d MMM yyyy",
"d MMM yy"
};
if (acceptableDateFormats.Any(format => DateTime.TryParseExact(date, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out dateTime))) {
return dateTime;
}
else {
return null;
}
}