I was playing around with the main differences between 4 monitor classes/approaches, like async/await, Task, BackgroundWorker and Thread), so I created some controls, which just interact with those 4 approaches/objects. I also wanted to dig into LINQ/LAMBDA at the same time and managed to write some successful LINQ-statements like this one, for example.
(from t in tabControlOutput.TabPages.Cast<TabPage>()
from c in t.Controls.OfType<WebBrowser>()
select c).ToList().ForEach(c => c.Navigate(Constants.BlankUrl));
Of course I was looking before posting this question and I found some nice information like this
Which redirects to a comment of Eric Lippert.
Besides that I am aware of LINQ delivering some overhead to standard-operations, which can be issued by standard instructions instead of linq, the overhead issue seems to be the particular reason fore MS, why stick to standard LINQ-less processing, but E.Lippert has some other arguments, especially referring to ForEach.
There are some statements, which seem to confuse me:
.... The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect.
I never heard / read about that in any programming book, best practice or whatever. Following common (and my) experience, the term sideeffects usually carries a negative taste of arbitrary , instead of being an aim, which is achieved willingly.
Could anyone clarifiy this quote from E.Lippert ?
Furthermore E.Lippert states, that some two lines seem to be harder to maintain, which I would not accept(ok, this might be opinion based).
But, regarding my piece of code, I can see that the only "ugly" thing is the "Cast".
So, would there be a reasonable reason(not opinion based, but in pure technical terms, arguments, restrictive statements, principles (readability also belongs to them), why my lines should or should not be replaced by traditional foreach, or even reflection ?
EDIT: I changed my code, remmed out my previous lines, made comments and added those lines. Please feel free to comment on them:
// ---> NOT RECOMMENDED APPROACH: Because LINQ is more designed to query, but at the end
// There is a modification of the queried objects, which
// Is not real LINQ, but a mix.
// And it violates the principles of least astonishment/surprise
// Changed to
var qresult = (from t in tabControlOutput.TabPages.Cast<TabPage>()
from c in t.Controls.OfType<WebBrowser>()
select c);
foreach (var tmp in qresult)
{
tmp.Navigate(Constants.BlankUrl);
}