Runtime: Unsafe.CopyBlock confusing parameter order

Created on 27 May 2019  路  2Comments  路  Source: dotnet/runtime

Unsafe.CopyBlock and Unsafe.CopyBlockUnaligned have the destination as first parameter and source as second. This is kinda confusing as all other copy methods in .NET have source as first and destination as second. Wouldn't it be better to follow the unofficial "standard" ?

Buffer.BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count);
Buffer.MemoryCopy (void* source, void* destination, long destinationSizeInBytes, long sourceBytesToCopy);
Array.Copy (Array sourceArray, Array destinationArray, int length);
File.Copy (string sourceFileName, string destFileName);

Unsafe.Copy<T> (void* destination, ref T source);
Unsafe.CopyBlock(void* destination, void* source, uint byteCount);

As you can see it's always source first, except on Unsafe.Copy methods.

area-System.Runtime.CompilerServices question

Most helpful comment

CopyBlock does follow the "unofficial" standard - that of the IL cpblk instruction that this method exposes.

All 2 comments

This would break all consumers of the API, so it can't be changed. The best thing you could do here (and is something that's recommended to do when a method has multiple parameters of the same type in a non-obvious order) is to use named arguments so that the compiler will do The Right Thing:tm: for you. Additionally, with this 7.2-introduced feature, you don't even need to name every argument.

Unsafe.CopyBlock(source: s, destination: d, 256);

CopyBlock does follow the "unofficial" standard - that of the IL cpblk instruction that this method exposes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chunseoklee picture chunseoklee  路  3Comments

noahfalk picture noahfalk  路  3Comments

Timovzl picture Timovzl  路  3Comments

btecu picture btecu  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments