我如何才能将此条件更改为我想要的

dor*_*rsa 6 c# asp.net

我有两个值的条件。如果等于0它的条件返回Absent,如果等于1它,则返回present。现在我想将第三个值添加到我的条件中。如果等于3它的条件返回Unacceptable absent

这是我的条件,具有两个价值:

(status >= 1 ? "Present" : "Absent")
Run Code Online (Sandbox Code Playgroud)

如何改变病情?

Joh*_* Wu 17

使用查找字典。

//Initialized once in your program
var lookup = new Dictionary<int,string>
{
    { 0, "Absent"},
    { 1, "Present"},
    { 3, "Unacceptably Absent" }
};

//Call this whenever you need to convert a status code to a string
var description = lookup[status];
Run Code Online (Sandbox Code Playgroud)


Joh*_*ica 14

使用嵌套三元运算符会为了简洁而牺牲可读性。我建议改用谦虚的switch语句:

string foo(int status)
{
    switch (status)
    {
        case 0:
            return "Present";
        case 1:
            return "Absent";
        case 3:
            return "Unacceptable absent";
        default:
            throw new ArgumentOutOfRangeException(nameof(status), $"What kind of person passes {status}?");
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 4

您可以将故障安全状态添加为“NA”并按如下方式执行操作:

status == 0 ? "Absent" : status == 1? "Present" : status == 3? "Unacceptable Absent" : "NA";
Run Code Online (Sandbox Code Playgroud)