How does the GetBytes function work? How does the GetBytes function work? arrays arrays

How does the GetBytes function work?


Yes, drop yours and use the standard library.

It is more efficient, because it copies both bytes at once by casting the byte array to a short pointer. To do this it requires fixed to allow the use of pointers, and hence the unsafe keyword.

Unsafe code is generally to be avoided in your own assemblies, as it means your code cannot be guaranteed safe, and so can only be run in fully trusted environments.

However, since Bitconverter is part of the standard .Net assemblies that are signed by Microsoft, Windows knows they are ok.

Library functions are less error prone too, because they have been battle hardened by millions of developers using them every day, whereas your function has been tested only by you.