【问题标题】:How would I convert this ternary expression into if else statements?如何将此三元表达式转换为 if else 语句?
【发布时间】:2017-09-13 19:38:27
【问题描述】:

我正在尝试找到一种更简单的方法将此三元表达式转换为 if else 表达式。

Account account = selection != 1 ? (Account)customer.Saving : (Account)customer.Checking;

【问题讨论】:

  • Account account; if (selection != 1) { account = (Account)customer.Saving } else { account = (Account)customer.Checking; } ...但是为什么呢?
  • 你确定你是这个编码的人吗?
  • 只是对等价的表达感到好奇。

标签: c# ternary-operator


【解决方案1】:

你可以写:

Account account;
if(selection != 1)
{
    account= (Account)customer.Saving
}
else{
    account= (Account)customer.Checking;
}

【讨论】:

  • 为什么要投反对票?我不认为我写错了什么。请解释谁曾否决过
  • 答案是正确的,加 1。我能想到否决票的原因是这个问题绝对是一个骗子而且质量低下。如果您回答问题,那么您就是在促进提问者和未来的访问者提出此类问题。
猜你喜欢
  • 1970-01-01
  • 2019-03-13
  • 2015-07-05
  • 1970-01-01
  • 2019-10-13
  • 2019-07-03
  • 2019-07-04
  • 2018-10-25
  • 2019-04-09
相关资源
最近更新 更多