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.

Recommended Answers

All 4 Replies

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.

True. The loop would be cleaner and more intuitive, off the top of my head.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.