If you want to do a memory copy between two arrays, there's an Array.Copy
function for that in .NET:
char[] GetCopy(char[] buf)
{
char[] result = new char[buf.Length];
Array.Copy(buf, result);
return result;
}
This is usually faster than manually for-looping to copy all the characters in buf
to result
, because it's a block copy operation that simply takes a chunk of memory and writes it to the destination.
Similarly, if I was instead given a char*
and an int
specifying its size, what are my options? Here are the ones I've considered:
- Buffer.BlockCopy: requires
src
anddst
to be arrays - Buffer.MemoryCopy: exactly what I'm looking for, but only available on .NET Desktop
- Marshal.Copy: stopped being supported in .NET 2.0
Or, if there's no alternative, is there some kind of way to construct an array from a pointer char*
and a length int
? If I could do that, I could just convert the pointers to arrays and pass them into Array.Copy.
I'm not looking for for-loops because as I said they're not very efficient compared to block copies, but if that's the only way I guess that would have to do.
TL;DR: I'm basically looking for memcpy()
, but in C# and can be used in PCLs.