@ njbuch - Personally, I find your function easier to read than the Linq solution. However, a solution based on char arrays would be even better for microcontrollers since it would avoid GC. I don’t have time at the moment to create one, though. I’m sure someone else already has. Also, I haven’t used the NETMF RegEx class much but I’d be curious to see how GC friendly a regex based solution to this problem would be.
@ njbuch - Actually your function always returns the same string as option or null.
So an even faster solution would be:
args)
{
foreach (var s in args)
if (s.IndexOf(option) == 0) return option;
return null;
}
or even
args)
{
foreach (var s in args)
if (s.IndexOf(option) == 0) return true;
return false;
}
Edit:
about readability of linq.
I think an actual programmer (even more if old School) will understand plain C# Code more easy.
But I think that linq Statements (no matter if with Extension methods or from, select, … Keywords) is more easy to understand for others, since it’s actually quite Close to a normal sentence.
If you get used to read it like this, t hen it’s quite easy.
What gets complicated is if the Statement gets too Long, since you have no results in the middle you can jump back as an reminder.
Yes that’s true, but I don’t blame this fully to ReSharper. Of course it could look which compiler is used and do not offer the new language features. But by this it also keeps my mind sharpened at what ReSharper actually does to my code.
I mean ReSharper is good, really good. but I would never trust it 100% for a commercial project.
I do not even trust myself a 100% for that (ReSHarper found already a lot of Bugs in my Code that only would have shown up in some Special cases), so I think me and ReSharper are a good team, looking at what each other is doing
8)
Maybe it’s pedantic, but what we’re discussing here isn’t LINQ at all. LINQ is a functional-ish DSL in C# that the compiler translates into calls to extension methods. What we’re talking about here is a few calls to those extension methods using the lambda syntax. It isn’t LINQ at all, it’s just plain old C# code. Out in the wild, actual LINQ code is vanishingly rare, in my experience.
Now, if I were to give my opinion on what’s more readable, it would depend on the situation, but unless things get pretty hairy, I think that the lambda syntax is actually very readable. It’s often possible to express an algorithm very elegantly and succinctly, which helps to get the idea across without burying it in a bunch of syntax.
The least readable code is anything done in the “fluent” style. Whoever thought that up has a serious brain problem, and ought to get that checked out.
@ godefroi - You are right, that the original Code in the 1st post isn’t actually linq, but this is what linq is translated to.
Some call it linq with lamdas and Extension methods, that’s why I call this linq too.
If this is actually wrong, I still don’t care