Main Site Documentation

Unsafe code


All sources state that unsafe code is not allowed on NETMF.

public static unsafe long DoubleToInt64Bits(double value)
            return *(((long*)&value));

Tick the “Allow unsafe code” in project properties and it works!
Further on I found out that actually the “fixed” keyword isn’t supported, pointers do work.

So I want to ask how dangerous is it to use code like the above, does the GC kick in after a method has finished, does it pause execution after A line of code or after an IL opcode.
Finally is this an undocumented feature/bug that will be different on other devices or even removed.


Edit: According to what I heard from the experts, using unsafe is not an option and can lead to a very-difficult-to-fix problems.


Would you mind shedding some light on exactly what goes wrong?


Garbage collector can kick in randomly and move objects that you are trying to access.


What if they are all on the stack? Would that be problematic.

EDIT: I’m just asking cuz I had a talk with some C# experts, and they all said as long as they are on the stack its okay because the GC doesn’t touch the stack.


And how did these expert say you can pit objects on stack? :slight_smile:

Anyway, unsafe is not supported


Local ValueTypes are on the stack, which is what the parameter and int variable are in the code I sent you.


oh yes your are right. I was thinking references on stack…many users confuse the reference with the actually object.