Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to out variables

Tags:

c#

I need to include double.TryParse(wordConf, out double wordConfDouble); in a script, but I get a feature out variable declaration is not available in c# 6 error message. Searching for it on Google, I can only see solutions for upgrading to C# 7 (which I am not allowed to do so in this project) so I wonder if someone could help me write an equivalent to this line that would work in any C# compiler.


2 Answers

You don't need to inline declare a type for out-parameters.

Replace:

double.TryParse(wordConf, out double wordConfDouble);

With:

double wordConfDouble;
double.TryParse(wordConf, out wordConfDouble);
like image 81
Eric McLachlan Avatar answered Mar 14 '26 16:03

Eric McLachlan


It's just the inline declaration which is not supported in < C#7.0. Change your code to

double wordConfDouble;
double.TryParse(wordConf, out wordConfDouble);

Reference: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#out-variables

like image 43
fubo Avatar answered Mar 14 '26 18:03

fubo