2

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

3
  • 5
    Did you set the language version in the project settings to C#7.0 or newer? Commented Aug 12, 2020 at 14:08
  • 1
    That syntax is not backwards compatible, of course. This is a trivial change that is not going to make any difference, though. Commented Aug 12, 2020 at 14:17
  • @UnholySheep I have it set to "C# latest major version (default)" and if expand the dropdown it shows I have up to C#7.3. Commented Aug 12, 2020 at 14:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.