【问题标题】:How to compare two conditons on a single line in a better way如何以更好的方式比较单行上的两个条件
【发布时间】:2019-11-06 03:46:09
【问题描述】:

这可以做得更好吗?

private bool IsEntityNeeded(EntityType type) => EntitiesRequiredToBuildComposite.Contains(type) && !RequirementsObtainedDict.ContainsKey(type);

RequirementsObtainedDictDictionary<EntityType, AbstractEntity> EntitiesRequiredToBuildCompositeList<EntityType>

如果两个条件都为真,我想返回一个布尔值,并且它必须在一行代码中。

我知道我可以进行比较并创建一个函数来执行此操作,然后调用所述函数,但这会创建更多代码行,这不是我想要的。

【问题讨论】:

  • 旁注:“RequirementsObtainedList 是字典”不是最好的命名理念
  • EntitiesRequiredToBuildComposite 作为 HashSet 可能会更好/更快

标签: c# dictionary boolean comparison equality-operator


【解决方案1】:

【讨论】:

  • 好主意!但我不需要空值检查 - 我找不到使用此方法在性能或空间上压缩我的方法的方法。
猜你喜欢
  • 1970-01-01
  • 2017-07-23
  • 2014-09-11
  • 1970-01-01
  • 2016-02-22
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多