首页 > 解决方案 > Is there a way that I can avoid an if statement full of checks

问题描述

My code looks like this:

 if (Settings.cc == CC.F1 || Settings.cc == CC.F2)

with additional tests for F3, F4 and F5

is there any way that I can avoid checking against Settings.cc for every entry?

标签: c#

解决方案


您可以执行以下操作:

if (new[] {CC.F1, CC.F2, CC.F3, CC.F4, CC.F5}.Contains(Settings.cc))

推荐阅读