11

Turns out
if(double.TryParse(input, out double value)
{
//Do something with value
}

can't be used in compilers using C# versions prior to 7.0. Reason: → out double value ←

...k den

Comments
  • 1
    Should it not be
    double value = default(double)

    if(double.TryParse(input, out value))
    {
    // something here
    }
  • 0
    @itsdaniel0 Well, prior to C# 7.0, yes. Now you can just do that if you only need "value" inside of this particular if-block.
  • 1
    @filthyranter Ah, I didn't know. That's pretty handy!
  • 1
    Oops, I accidentally forgot a closing ) for the if clause in my rant and noticed it way too late. woopsies
  • 0
    (at least that made me suggest a feature for devRant)
  • 0
    Actually not really correct. The variable declared that way is valid in the scope of the if, not the "then" scope.
    This enables you to invert it like
    if(!double.TryParse("5", out double d))
    throw new IAmAngryException();
    //do something with the d
  • 0
    @Firedragonweb What's wrong then?
Add Comment