4 ms·
Always accessing array.Length is defensively coding. In the event your array is mutated, always accessing array.Length ensures you won't run into an Index Out o
by ducttape12 7y ago
Always accessing array.Length is defensively coding. In the event your array is mutated, always accessing array.Length ensures you won't run into an Index Out of Bounds exception.
Even better is to just avoid accessing the array's length. I almost always use foreach or Linq.
- gameswithgo 7y agoThat is orders of magnitude slower and allocates. Of course you can use: https://github.com/jackmott/LinqFaster https://github.com/jackmott/LinqFaster
- gwbas1c 7y agoWrong From the article: > It also turned out that Foreach often walks through the array faster than For
- merb 7y ago1. `ForEach` is slower. 2. `foreach` is faster. 3. Linq is slower.
- ducttape12 7y agoI write first for readability and performance second. Not to say that performance isn't important (and of course I pay attention to O notation), but throwing a little more memory and CPU power at a web page is far cheaper and easier than spending developer time reading code.