-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend throw
syntax to include a conditional check
#14924
Comments
I think that throw expressions (coming in C#7) do what you want: return string.IsNullOrEmpty(userId) ? "no userid"
: string.IsNullOrEmpty(address) ? throw new Exception()
: string.IsNullOrEmpty(name) ? throw new Exception()
: //...other code (if you refactor it into expression) |
|
If we rationalise the original code, then the three versions (including from #119) would be: public string QueryOrderNo(string userId, string address, string name)
{
if (IsNullOrEmpty(userId)) return "no userid";
if (IsNullOrEmpty(address)) throw new Exception();
if (IsNullOrEmpty(name)) throw new Exception();
//...other code
}
public string QueryOrderNo(string userId, string address, string name)
{
return IsNullOrEmpty(userId) ??? "no userid";
throw IsNullOrEmpty(address) ??? new Exception();
throw IsNullOrEmpty(name) ??? new Exception();
//...other code
}
public string QueryOrderNo(string userId, string address, string name)
requires !IsNullOrEmpty(userId) else return "no userid"
requires !IsNullOrEmpty(address) else throw new Exception()
requires !IsNullOrEmpty(name) else throw new Exception()
{
//...other code
} which begs the question, why? It can be argued that the |
More like: public string QueryOrderNo(string userId, string! address, string! name)
requires address != string.Empty
requires name != string.Empty
{
if (string.IsNullOrEmpty(userId)){
return "no userid";
}
//...other code
} |
Hello @mrhuo, That is a interesting idea to help make method argument validation easier. That is something that alot of people want to see in C# and it has been discussed for a long time. If you could please read through the discussion raised in #119 and see if you like the design made there. If you think that you like your design better than please try to expand on it with some more specifics about how exactly the compiler should treat the new ??? operator. Take a look at this document that explains how to create a proposal. As dsaf said, it helps everyone if you can give the issues you create here a name that is descriptive so that people can find it easily. And be sure to always search Area-Language-Design for other discussions that might have taken place already. Thanks for contributing. |
@zippec Thanks for reply to me, but your solution not for I want, because I want check condition first, and then run other code. but your code return outter to the method directly. |
@DavidArno First, thank you reply, I'm sorry to have kept you so confused. I just want code shorter , more readable, more graceful, like a artwork. |
@dsaf Thanks for your reply and your tip to me.
Destroy the beauty of the C# style, like "Gangnam style" song with “little apple”'s dance. |
throw
syntax to include a conditional check
Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks! |
I would like to propose a new syntax, the existing similar code as follows:
As you see, like more more if keywords..
Can we like this?
Is help for your?
The text was updated successfully, but these errors were encountered: