简体   繁体   English

检查整数然后返回字符串

[英]Checking an integer then returning a string

So I have a small snippet of code that I may use when I want to quickly check an integer before deciding what a string value will be: 因此,当我想在确定字符串值是什么之前快速检查整数时,可以使用一小段代码:

string status = (statusID == 0 ? "Inactive" : "Active");

However, I cannot remember what this practice / piece of code is called of referred to as. 但是,我不记得此实践/代码称为什么。 I wanted to implement a similar bit of code, but with two parameter checks, to check for two different numbers, giving three possible outcomes. 我想实现类似的代码,但是要进行两次参数检查,以检查两个不同的数字,从而得出三个可能的结果。 Is this possible? 这可能吗? Or would it be more suitable to expand this out into two usages of this code, checking for a certain string, or expand the functionality into a method? 还是更适合将其扩展为该代码的两种用法,检查某个字符串或将该功能扩展为一个方法?

?: is conditional operator in c#: ?: Operator (C# Reference) ?:是c#中的条件运算符: ?:运算符(C#参考)

Just put another ?: statement in else part of the first one: 只需在第一个语句的其他部分添加另一个?:语句:

string status = (statusID == 0 ? "Inactive" : (statusID == 1 ? "Active" : "OtherOne"));

That's gonna return "Inactive" for statusID == 0 , "Active" for statusID == 1 and "OtherOne" for others. 那将为statusID == 0返回“无效”,对于statusID == 1返回“有效”,而对于其他返回“ OtherOne”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM