【问题标题】:how to apply if condition in C# [duplicate]如何在 C# 中应用 if 条件 [重复]
【发布时间】:2025-12-17 22:45:01
【问题描述】:
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

【讨论】: