"out return" - an even more shortcutty version of "out var" #2779
Unanswered
ekolis
asked this question in
Language Ideas
Replies: 3 comments
-
This seems like such a short (and rare) coding pattern that it doesn't really need anything to help out here. Indeed, why not just make it: public static bool ToBool(this string value)
=> bool.TryParse(value, out var result) && result; ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
consider this case: public static bool? ToBoolOrDefault(this string value)
{
if (!bool.TryParse(value, return result))
{
return null;
}
} Should the compiler tell you "not all code paths return a value"? |
Beta Was this translation helpful? Give feedback.
0 replies
-
So: public static bool ToBoolOrDefault(this string value)
=> bool.TryParse(value, out var result) ? result : (bool?)null; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Use case:
versus the existing two line version of this method:
maybe even
out
could be implied byreturn
?Beta Was this translation helpful? Give feedback.
All reactions