Rezgar holds his own tail; Is this right the first time or the last?

I had a few detailed 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 proposed for elegance with a LINQ expression as follows:

 return this.Controls.OfType<ComboBox>().Any(c => (c as ComboBox).SelectedIndex == -1); 

... but a subsequent inspection of Resharper says about the code that it generated (above): “The cast type is redundant” (referring to the “c as ComboBox” part), so it ends up like:

 return this.Controls.OfType<ComboBox>().Any(c => c.SelectedIndex == -1); 

Should Resharper generate Resharper-approved code? Or is it just that sometimes it takes two passes to completely “dishonor your lower backs”?

+6
source share
1 answer

Yes, sometimes ReSharper corrects itself by requiring a second pass to get it "just right." I have always assumed that it uses certain “safe templates” for conversion, and in some cases some parts of safe conversion are not really needed.

All versions of the code are correct and equivalent, although the first "pass" is converted to Linq, and the second "pass" removes some redundant code that the Linq transformation added.

+9
source

Source: https://habr.com/ru/post/926342/


All Articles