1

I am retrieving values from my array using arr.GetValue(5, 6, 7, 8), arr.GetValue(10, 11, 12, 13) many times.

Is there a shorthand for this like arr.Get(offset, count) so that I can use arr.Get(5, 4), arr.Get(10, 4) to get the smaller array from the bigger array?

Elmo
  • 6,409
  • 16
  • 72
  • 140

1 Answers1

4

Use Skip and Take to jump around an enumerated list:

var slice = arr.Skip(5).Take(4);

You may find some different answers and more info at these questions:

Community
  • 1
  • 1
Andrew
  • 4,953
  • 15
  • 40
  • 58
  • 1
    Just curious... are these methods optimized for arrays, or does `Skip` have to walk through the first 5 values in this example? – adv12 May 13 '14 at 17:50
  • 1
    I'm not sure, but as far as I can tell the answer is no, they are not optimized. You can read an article by Jon Skeet about these methods here: https://msmvps.com/blogs/jon_skeet/archive/2011/01/02/reimplementing-linq-to-objects-part-23-take-skip-takewhile-skipwhile.aspx – Andrew May 13 '14 at 17:57
  • 1
    @adv12 If you want a blindingly fast method, use Array.Copy as shown in the first of Andrew Arnold's links. – Andrew Morton May 13 '14 at 21:14