我有一些冗长的代码:
private bool AnyUnselectedComBox() { bool anyUnselected = false; foreach (Control c in this.Controls) { if (c is ComboBox) { if ((c as ComboBox).SelectedIndex == -1) { anyUnselected = true; break; } } } return anyUnselected; }
…… Resharper提供了一个像这样的LINQ表达式的elegantize:
return this.Controls.OfType<ComboBox>().Any(c => (c as ComboBox).SelectedIndex == -1);
…但随后的Resharper检查说明了它生成的代码(上图):“类型转换是多余的”(指“c作为ComboBox”部分),因此它最终为:
return this.Controls.OfType<ComboBox>().Any(c => c.SelectedIndex == -1);