Why are out parameters inside an if statement condition not local to the if block? [Answered]
Is this because of how it ends up compiling val above the if statement?
7 Replies
You can access
val
there because the if (!TryGetValue) return;
pattern is super common in .NET, and we wanted that pattern to work with out variable declarationsI see, so in that case when it fails, you wouldn't have to call it again to access the out parameters. That makes sense. Thanks!
Or declare them ahead of time, like
I feel like that's more explicit, but I can see how that could get annoying.
Well, that was the old way
But C# 6 (iirc) added inline out variable declaration, and we wanted it to work for all scenarios
The leaky scoping was controversial, but imo for the best
Ahh didn't know it used to be like that. If it was to make it work for all scenarios, then I totally agree.
✅ This post has been marked as answered!