Create conditional return or throw #7962
Replies: 3 comments 3 replies
-
Use a switch expression instead public class MyClass
{
public bool MyValidation(bool var1, bool var2, string someText)
{
return (var1, var2, someText) switch
{
(true, true, _) => true,
(false, false, _) => false,
(_, _, var text) when string.IsNullOrEmpty(text) => throw null!
_ => throw null!
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
I put strong down votes about changing the order of conditions like that. It's way too Englism or European-ism. If the condition is tested first, it should appear at first. This is the nature order that fits execution order. |
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
-
Summary
I know that this has been passed before, but the last active one was from 2019, so maybe times have changed?
I would like to make a nice compact syntax to return a value or throw an exception based upon a condition
Motivation
Often developers are stuck writing if statement to make exits early on in a method by returning a value or throwing an error.
For example
The language already made this a bit better by onelining these situations
For example
Detailed Design
BUT... what is we could do this
this is even more intresting when we add pattern matching
for example
Beta Was this translation helpful? Give feedback.
All reactions