本文介绍了单行如果有2个动作声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的单行if语句超过1行动
I'd like to do a single line if statement with more than 1 action.
默认是这样的:
(if) ? then : else
userType = (user.Type == 0) ? "Admin" : "User";
不过,我并不需要一个其他而已,我需要一个否则,如果
But I don't need an "else" only, I need an "else if"
一样,在多行:
if (user.Type == 0)
userType = "Admin"
else if (user.Type == 1)
userType = "User"
else if (user.Type == 2)
userType = "Employee"
有没有在单行的可能?
推荐答案
听起来像是你真的想要一个词典< INT,串>
也可能是开关
语句...
Sounds like you really want a Dictionary<int, string>
or possibly a switch
statement...
您的 的可与运营商有条件做到这一点,但:
You can do it with the conditional operator though:
userType = user.Type == 0 ? "Admin"
: user.Type == 1 ? "User"
: user.Type == 2 ? "Employee"
: "The default you didn't specify";
虽然你的可能的提出,在一条线,我强烈敦促你不要
While you could put that in one line, I'd strongly urge you not to.
我通常的只有的做到这一点不同的条件,虽然 - 不只是几个不同的可能值,这是更好的处理地图。
I would normally only do this for different conditions though - not just several different possible values, which is better handled in a map.
这篇关于单行如果有2个动作声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!