c# - 重构方法 (C#)
问题描述
我有方法:
private bool MyMethod(PlantType plantType)
{
return plantType.PlantMoveType == PlantMoveType.PlantReady
|| plantType.PlantMoveType == PlantMoveType.PlantRelase
}
我可以把它写成其他方式吗?也许与LINQ?
解决方案
一种方法是将要检查的枚举值放入数组中,然后调用Contains
.
return new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
如果您使用的是 C# 7 或更高版本,您还可以将方法编写为expression-bodied:
private bool MyMethod(PlantType plantType) =>
new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
推荐阅读
- r - 将值附加到 for 循环中的同一列 [R]
- python - 如何从图像A中随机裁剪多边形并将其粘贴到python中的图像B上?
- ruby-on-rails - 如何确定主动存储关联?
- sql - Hive-如何获取派生列名称并在同一查询中使用它?
- python - Python - 如何在嵌套列表上实现 zfill
- javascript - Javascript测验计时器
- adobe - inDesign CC:使用脚本将带有主跨页的页面添加到文档
- r - 从 R 写入 sql 表时出现未知错误
- linux - 双击时Python3 GUI脚本不起作用
- amazon-web-services - 如何在 AWS API Gateway 中生成 Set-Cookie 集成响应标头?