【问题标题】:how to apply if condition in C# [duplicate]如何在 C# 中应用 if 条件 [重复]
【发布时间】:2018-08-16 11:15:22
【问题描述】:
if (excellInfoUpdateL3.canSendLetter == "N/A") {
  return ClosureInfo.canClose ="N";
}
else {
  return ClosureInfo.canClose = excellInfoUpdateL3.canSendLetter;
}

上面的代码可以用if条件或类似的东西写在一行吗?

【问题讨论】:

  • 嗯?你需要编辑你的答案,这种格式不可读

标签: c#


【解决方案1】:

你可以这样放在一行中:

return excellInfoUpdateL3.canSendLetter == "N/A" ? "N" : excellInfoUpdateL3.canSendLetter;

【讨论】:

  • 或者,稍微好一点,这个return returnClosureInfo.canClose = excellInfoUpdateL3.canSendLetter == "N/A" ? "N" : excellInfoUpdateL3.canSendLetter;
【解决方案2】:

是的,你可以使用三元运算符

return (excellInfoUpdateL3.canSendLetter == "N/A")?"N":excellInfoUpdateL3.canSendLetter;

这是三元运算符的工作原理

(Condition)?if contion is true : if condition is false

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 2016-03-08
    • 2021-11-19
    • 2020-10-02
    • 2019-12-28
    • 2012-04-30
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    相关资源
    最近更新 更多