Reputation: 10267
I had some verbose code:
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;
}
...that Resharper offered to elegantize with a LINQ expression like so:
return this.Controls.OfType<ComboBox>().Any(c => (c as ComboBox).SelectedIndex == -1);
...but the subsequent Resharper inspection says about the code it generated (above): "Type cast is redundant" (referring to the "c as ComboBox" part), so that it ends up as:
return this.Controls.OfType<ComboBox>().Any(c => c.SelectedIndex == -1);
Shouldn't Resharper generate Resharper-approved code? Or does it simply sometimes need two passes for it to fully "gird up its loins"?
Upvotes: 6
Views: 282
Reputation: 180927
Yes, sometimes ReSharper corrects itself, requiring a second pass to get it "just right". I've always assumed it uses certain "safe templates" to do the conversion and in some cases some parts of the safe transformation aren't really needed.
All versions of the code are correct and equivalent though, the first "pass" is converting to Linq and the second "pass" removes some redundant code that the Linq transformation added.
Upvotes: 9