Welcome! and rules
Joel on Software
Is unsafe more efficient?
It seems plausible that unsafe code (*p++, etc.)
As far as I know, this relates to code being verifiable or not, and how the framework loads its assemblies. I am no expert on this, but my understanding so far is that code that is managed is typically verifiable, meaning that when the assembly is loaded it is checked by the load-time verifier. This verification checks all the types used along the execution control paths adhere to the types etc - so that the garbage collector can work etc....this verification point to me seems to be were the loss of performance would occur. Unmanaged code basically means no verification is performed.
Unsafe definitely does NOT mean unmanaged.
Yes you are right - further reading explains that unsafe is still managed by the CLR.
...and it all get JIT compiled to native code so why should it be any slower -- unless you tend to tweak your x86 code by hand anyway.
There's no reason why Unsafe code should be any faster than the equivalent safe code.
Thanks. Unsafe may not be what I thought.
Unsafe blocks primarily exist so that C# code can do interop with unmanaged libraries; particularly older C librarys that do a lot of pointer manipulation, pass around raw blocks of untyped bytes, that sort of thing. It could also be used for things like twiddling bytes in a memory buffer - like a DirectX surface for example.
Fog Creek Home