【发布时间】:2018-09-18 18:05:36
【问题描述】:
return ship.DefenseType?.PropulsionMethod != null
? new BattleMethod(ship.DefenseType.PropulsionMethod)
: null;
嗨,我上面的当前 return 语句正在返回一个 Propulsion 方法,如果它不为空的话。但是,我的数据库有不同类型的 由字段中的前 2 个字母表示的推进方式(PK、PA、PT 等)。
在进一步进入 return 语句之前,如何检查以确保 PropulsionMethod 以“PK”开头?
在伪代码中,它可能看起来像这样:
if (ship.DefenseType?.PropulsionMethod).startsWith("PK")
&& ship.DefenseType?.PropulsionMethod != null)
{
return new BattleMethod(ship.DefenseType.PropulsionMethod)
}
else
{
return null;
}
我试过了
return ship.DefenseType?.PropulsionMethod != null &&
ship.DefenseType?.PropulsionMethod.StartsWith("PK")
? new BattleMethod(ship.DefenseType.PropulsionMethod)
: null;
但我收到此错误:
运算符 && 不能应用于 bool 和 bool 类型的操作数?
【问题讨论】:
-
制作工厂类
-
您的解决方案有什么问题?在什么情况下它不起作用?