Refactor C# unsafe method I have the following two unsafe methods that I would like to refactor:

public static unsafe void UnsafeMethod(byte[] array, int pos, short value)
{
    fixed (byte* numRef = &amp;(array[pos]))
    {
        *((short*)numRef) = value;
    }
}

public static unsafe void UnsafeMethod(byte[] array, int pos, int value)
{
    fixed (byte* numRef = &amp;(array[pos]))
    {
        *((int*)numRef) = value;
    }
}


If I want to make the method managed will the following conversion be right ?

public static void UnsafeMethod(byte[] array, int pos, short value)
{
    array[pos] = value;
}
public static unsafe void UnsafeMethod(byte[] array, int pos, short value)
{
    fixed (byte* numRef = &amp;(array[pos]))
    {
        *((short*)numRef) = value;
    }
}


is not 

public static void UnsafeMethod(byte[] array, int pos, short value)
{
    array[pos] = value;
}


Explanation: 
&amp;(array[pos]) gets the address of the memory of the "pos"th element of the array and byte* numRef = &amp;(array[pos]) makes that the pointer numRef points to these address.
if you do *numRef = value you write value into the address numRef is pointing -- which is the "pos"th element of the array. 
But you lose precision, because value is short (2 Byte) but you write into a byte (1 Byte) address.

if you cast numRef -- which is a pointer to a byte (= ONE byte) -- into a short (= TWO byte) then the pointer points to the same address BUT the pointer is 2 Byte "wide". 
What i mean with "wide" is that if numRef points to 0x0 and you do numRef++ then numRef will point to 0x2 because it is short and short is 2 Byte big.

We have this short* numRef and you want to do *numRef = value then you will write in the memory of the "pos"th element of the array AND in the "pos + 1"th element of the array. so this method is not:

array[pos] = value


it is something like (i can't say that exactly because i am not the compiler):

array[pos] = upperByte(value)
array[pos + 1] = lowerByte(value)




Grafical Explanation:

May this be your array:

Address in RAM| 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | 0x08 | 0x09 |
Array Indexer | [0]  | [1]  | [2]  | [3]  | [4]  | [5]  | [6]  | [7]  |
Content       | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 |


and pos is 2.
and value is this: value = 0x1234

so the pointer numRef points to 0x04:

print(numRef); // Ouptup will be 0x04


and looks like this:

Pointer byte* numRef      --&gt;      &lt;--
Address in RAM| 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | 0x08 | 0x09 |
Array Indexer | [0]  | [1]  | [2]  | [3]  | [4]  | [5]  | [6]  | [7]  |
Content       | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 |


and after *numRef = value the array looks like this:

Pointer byte* numRef      --&gt;      &lt;--
Address in RAM| 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | 0x08 | 0x09 |
Array Indexer | [0]  | [1]  | [2]  | [3]  | [4]  | [5]  | [6]  | [7]  |
Content       | 0x00 | 0x00 | 0x34 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 |




if you cast it to short then you have this:

Pointer short* numRef     --&gt;             &lt;--
Address in RAM| 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | 0x08 | 0x09 |
Array Indexer | [0]  | [1]  | [2]  | [3]  | [4]  | [5]  | [6]  | [7]  |
Content       | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 | 0x00 |


and after *numRef = value the array looks like this:

Pointer short* numRef     --&gt;             &lt;--
Address in RAM| 0x02 | 0x03 | 0x04 | 0x05 | 0x06 | 0x07 | 0x08 | 0x09 |
Array Indexer | [0]  | [1]  | [2]  | [3]  | [4]  | [5]  | [6]  | [7]  |
Content       | 0x00 | 0x00 | 0x12 | 0x34 | 0x00 | 0x00 | 0x00 | 0x00 |




How it could be implemented (not recommended): 


  if you use this methods PLEASE test it. with different system (32 / 64
  bit). 


for the public static unsafe void UnsafeMethod(byte[] array, int pos, short value):

public static void SafeMethod(byte[] array, int pos, short value)
{
    array[pos] = value &gt;&gt; 8;
    array[pos + 1] = value &amp; 0xFF;
}


and for the same method with int (and here is the problem - is this int 64 or 32 bit?!) it could look like this:

// For 64 bit integer
public static void SafeMethod(byte[] array, int pos, int value)
{
    array[pos    ] = (value &gt;&gt; 24) &amp; 0xFF;
    array[pos + 1] = (value &gt;&gt; 16) &amp; 0xFF;
    array[pos + 2] = (value &gt;&gt; 8) &amp; 0xFF;
    array[pos + 3] = value &amp; 0xFF;
}


Legend:

value &gt;&gt; x &lt;=&gt; shift the bits of value x times right
value &lt;&lt; x &lt;=&gt; shift the bits of value x times left
value &amp; 0xFF &lt;=&gt; value &amp; 0b11111111 &lt;=&gt; logical bitwise and