如何使用条件直接分配bool变量

时间:2014-01-29 11:44:49

标签: c# asp.net .net if-statement

我真的很擅长将这个条件赋给bool变量。

比方说,我有这个

bool type;

if (((sDataSet.Shipment[0].CI_TYPE == Constants.ShipType.CI_R) &&
                         (shipType == Constants.ShipType.CI_R)) ||
    ((sDataSet.Shipment[0].CI_TYPE == Constants.ShipType.CI_P) &&
                         (shipType == Constants.ShipType.CI_R)) ||
    ((sDataSet.Shipment[0].CI_TYPE == Constants.ShipType.CI_R) &&
                         (shipType == Constants.ShipType.CI_P)))
    type = true;
else 
    type = false;

如何在bool类型中直接指定此条件?优化或更好的方式。

1 个答案:

答案 0 :(得分:1)

只需将您的变量分配给if语句,但删除if。像这样:

type = (((sDataSet.Shipment[0].CI_TYPE == Constants.ShipType.CI_R) &&
                             (shipType == Constants.ShipType.CI_R)) ||
            ((sDataSet.Shipment[0].CI_TYPE == Constants.ShipType.CI_P) &&
                            (shipType == Constants.ShipType.CI_R))  ||
            ((sDataSet.Shipment[0].CI_TYPE == Constants.ShipType.CI_R) &&
                            (shipType == Constants.ShipType.CI_P)))