I was wondering if there was an existing method within the .Net frame work which will copy every nth element of an array to a differant array with an initial offset e.g.

Original Source Array (Bytes)

Original Target Array

Source Offset 1
Source Step 3

Target Offset 3

Updated Target Array

I can quite easily do this with a loop, however it would be handy if a method existed which performed this operation. Primerily the array is a single dimensioned byte array.

A loop is simple enough, but you can LINQize it fairly easily:

var everyNth = src.Where((x, i) => i % n == 0);

That gives you the elements, and copying them to another array is trivial.

I thought about LINQ, but if I needed to use a loop to copy the results of everyNth I may aswell just copy the values between the arrays in the loop it's self.

It's kind of one of those situations where LINQ would solve it nicely, but the code for either LINQ or just regular loops would be approximately the same amount of code. I don't think there are any other ways of implementing specific functionality like this in the .NET framework, no already coded methods.

This article has been dead for over six months. Start a new discussion instead.