0

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)
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30

Original Target Array
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00

Source Offset 1
Source Step 3

Target Offset 3

Updated Target Array
00,00,00,12,00,00,00,16,00,00,00,20,00,00,00,24,00,00,00,28

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.

3
Contributors
4
Replies
31
Views
2 Years
Discussion Span
Last Post by overwraith
0

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.

0

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.

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.