Alternative to if, else if

后端 未结 8 1050
后悔当初
后悔当初 2021-02-07 11:20

I have a lot of if, else if statements and I know there has to be a better way to do this but even after searching stackoverflow I\'m unsure of how to do so in my particular cas

8条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 11:51

    What you want is a Dictionary:

    Dictionary mapping = new Dictionary();
    mapping["SWGAS.COM"] = "Southwest Gas";
    mapping["foo"] = "bar";
    ... as many as you need, maybe read from a file ...
    

    Then just:

    return mapping[inputString];
    

    Done.

提交回复
热议问题