Inline Variable Declaration Failing to Build

Viewed 102

On a project I took over, there was this extension method:

public static DateTime? TryParseDateTime(this string val)
{
    DateTime date;
    return DateTime.TryParse(val, out date) ? date : (DateTime?)null;
}

Personally, I like to use inline variable declarations when possible so I updated this method to:

public static DateTime? TryParseDateTime(this string val)
{
    return DateTime.TryParse(val, out DateTime date) ? date : (DateTime?)null;
}

This project is in VS 2017 and the Error list shows no errors, but the project fails to build. When I check the output window, I see an error is occurring in this method with the message:

2>C:...\Extensions.cs(35,56,35,60): error CS1003: Syntax error, ',' expected

I am not clear on what this error is referring to since this error only appears when I actually try to build. I also tried cleaning the solution and rebuilding but the error persists. Any insight into the cause of this would be greatly appreciated.

Thank You

0 Answers
Related