|
What's wrong with foreach?
|
|
|
|
|
Not as efficient as a for for the same task.
|
|
|
|
|
OK, but that may be a bit outdated - for example, the difference between
private static int test1(int[] ar)
{
int sum = 0;
foreach (var i in ar)
{
sum += i;
}
return sum;
}
and
private static int test2(int[] ar)
{
int sum = 0;
for (int i = 0; i < ar.Length; i++)
{
sum += ar[i];
}
return sum;
}
Is .. not much. The loop bodies are
_loop1:
mov eax,dword ptr [rdx+r9+10h]
add ecx,eax
inc r8d
add r9,4
cmp r8d,r10d
jl _loop1
_loop2:
mov eax,dword ptr [rdx+r9+10h]
add ecx,eax
inc r8d
add r9,4
cmp r8d,r10d
jl _loop2
Literally the same thing, it even used the same registers.
|
|
|
|
|
Now try the for in reverse. And try it with something more complex than an array of int s.
|
|
|
|
|
Well you can't foreach in reverse so..
Obviously for is more general.
|
|
|
|
|
foreach is more general in that it can work on any IEnumerable , whereas for is more general in that it doesn't require an IEnumerable . ::shrug::
I find that for , particularly a reverse for (which reduces calls to Length or Count or whatever), can perform much better than foreach . Though the actual time saved may not be significant, still you don't know what else is going on in the system.
On why to use DataViews[^]
|
|
|
|
|
harold aptroot wrote: What's wrong with foreach?
The young'uns prefer using linq for everything these days. Apparently unreadable, single-line, non-debugabble code using slow features like anonymous methods is the way to go 
|
|
|
|
|
* Static initializers - I can't imagine using it for Console.WriteLine or something (we always type cw{Tab} anyway, right?) And there's a problem that some methods don't have an intuitive name without the class name, like Tuple.Create . But I like that I can now import extension methods from classes instead of namespaces.
* Auto property initializers - Very neat. Should have been there from the beginning.
* Dictionary initializers - I guess it's a more intuitive way of writing this particular code but was it absolutely necessary? No. Now we just have 2 slightly different ways of writing the same thing.
* String interpolation - I agree it doesn't look pretty, but the old way ({0},{1},{2},etc) didn't look pretty either.
* nameof() - Very small feature, but it makes sense to use it everywhere you can.
* Expression-bodied members - I think this is a confusing feature. It doesn't even save that much typing. Methods don't look like methods anymore and properties no longer have a 'get' keyword.
* Exception filters - Should be useful on rare occasions, but will be abused more often.
* Await in catch/finally - Without it, async/await is an incomplete feature, so it should be there, but I don't expect to use it much.
* Null conditional - This takes away a lot of ugly, boring, annoying code. Very nice. But it will make C# more cryptic for novice programmers.
|
|
|
|
|
Static Using Syntax - I think its usefulness really depends on the use-case. If you have a class that makes heavy use of Console or static members in Math , it can be beneficial; otherwise, things might get weird easily.
Auto-Property Initializers - I don't use much of the syntactic sugar since C# 3.0, but it's good for people who like it because this feature makes auto properties complete.
Dictionary Initializers - I don't really care.
String Interpolation - Looks better than the String.Format syntax but can be problematic when it comes to localization. Still, I like it a lot.
nameof() Expression - A small but very welcome feature, I use it everywhere I can. This one really shines in Roslyn-based refactorings.
Expression-bodied Methods and Properties - I don't really care, not too beneficial.
Exception Filters - I guess this one has been supported by the CLR ever since, now it is exposed by C#. Can be useful, but like many others I see a large abuse vector.
Await in Catch/Finally - I don't do much async stuff, but it makes the whole feature complete.
Null-Conditional Operator - Surely a nice to have shortcut, but it makes the code harder to read, especially for people who are not used to it.
I think this version of C# brought some very welcome additions, but it's also beginning to get seriously bloated. Too much choices and syntactic sugar can become problematic and a mess if you're working on projects with different kinds of coding guidelines.
|
|
|
|
|
So, I was just looking at this tip/trick - Sql Query Execution Order[^] and, okay, the spelling isn't great but the content is interesting enough - thought it worth a 3 (it could be expanded and felt like he'd cut it short). However, prior to that it had two 5 votes and two 1 votes. A little extreme. Perhaps voting should be tied to a comment - I know we've had this debate before but if the two 1 voters thought the post that bad, they should be prepared to say why or is the author expected to guess what might have caused them to vote that way? On the other hand, are the two 5 voters the author and a secondary account? We need to know these things, we demand to know!!!
|
|
|
|
|
been there done that (vote and comments).
Voting 1 or 5 and adding a "no comment" comment is more useless than no comment at all.
I hate witch hunting of all sorts (more so on the internets).
I'd rather be phishing!
|
|
|
|
|
Maximilien wrote: I hate witch hunting of all sorts (more so on the internets).
I realize that there is no real cure for this malaise. 
|
|
|
|
|
I disagree, I have some idiot commented an article down vote with a . I at least know who the person is and can discount the input. CM was kind enough to reverse the value and I refrained from returning the favour.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Ooh; forgot about the always excellent Pinal Dave and I think that looks identical to the tip/trick. How strange! Thanks for spotting that.
|
|
|
|
|
According to a very small number of members here, downvotes and 1 votes require no comment or initiator name. Punishment should be anonymous - whatever the stinky elephant that means. 
|
|
|
|
|
In the not too distant past vote 3 and below forced you to comment - for several reason Chris decided to remove it...
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: for several reason Chris decided to remove it.
I know. I still don't understand why.
|
|
|
|
|
Will a pessimists blood type always B-Negative?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
:groan: that one was anemic!
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
|
Possibly, but it can't all be in vein.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Ravi Bhavnani wrote: Bloody hell!
Straight from the heart.
|
|
|
|
|
Logic dictates that T Negative must also be taken into account.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|