1

I'm working on a method to try to get a date from a string.

First I had this code:

public static DateTime? TryGetDate(this string date) 
{
    DateTime? outputDate;

    if(DateTime.TryParse(date, out outputDate)) 
    {
        return outputDate;
    }

    return null;
}

This worked fine but then I saw a suggestion on my Visual Studio telling me to change it to:

public static DateTime? TryGetDate(this string date) 
{
    if(DateTime.TryParse(date, out var outputDate)) 
    {
        return outputDate;
    }

    return null;
}

But after that change, I got a

Syntax error, ',' expected

on the TryParse line of code.

Why did this happen? I got a suggestion with VS but applied that suggestion and I got a compilation error? The project Target Framework version is v4.6.2

1
  • 10
    This is a C# 7 language feature. The error you're getting implies your project targets a lesser version. Commented Sep 9, 2019 at 18:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.