Earlz
Earlz

Reputation: 63835

How to fastly copy only a portion of an array to another array?

I'm writing a high-performance data structure. One problem I came across is there doesn't seem to be anyway to copy only a portion of an array to another array (preferably as quickly as possible). I also use generics, so I'm not really sure how I'd use Buffer.BlockCopy since it demands byte addresses and it appears to be impossible to objectively determine the size of an object. I know Buffer.BlockCopy works at a byte-level, but does it also count padding as a byte?

Example:

var tmo=new T[5];
var source = new T[10];
for(int i=5;i<source.Length;i++)
{
  tmp[i-5]=source[i];
}

How would I do this in a faster way like Array.CopyTo?

Upvotes: 0

Views: 95

Answers (1)

recursive
recursive

Reputation: 86084

You can use Array.Copy().

Array.Copy(source , 5, tmp, 0, tmp.Length);

Upvotes: 2

Related Questions