Feature request: var out keyword scoped to If blocks #3468
Replies: 5 comments
-
This seems too arbitrary and too easy to miss. |
Beta Was this translation helpful? Give feedback.
-
It could be done by switch. switch (20)
{
case var value when value > 5:
{
Console.WriteLine(value);
break;
}
} |
Beta Was this translation helpful? Give feedback.
-
It can also be done with extra braces: public static Main(string[] args)
{
{
if (20.IsGreaterThanFive(out var value))
{
Console.WriteLine(value); // Prints 20.
}
// value exists only in this scope.
}
} |
Beta Was this translation helpful? Give feedback.
-
I'm not opposed to a feature to constrain scope as much as I'm opposed to |
Beta Was this translation helpful? Give feedback.
-
The design decision that variables declared by out var would "leak" out to the enclosing scope was extremely heavily debated at the time. I would anticipate that LDM members would have no desire at all to revisit the issue again, especially given that there were good reasons for the current decision and easy workarounds to limit scope (as detailed above). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This proposal is quite simple.
Consider adding a
var out
keyword which creates a variable in the scope of an if condition.Example
This can be used as follows
Safely Unwrapping Optionals
The
var out
modifier will only be allowed in an if condition and must be scoped to the if block.Beta Was this translation helpful? Give feedback.
All reactions