I haven't seen much examples of unsafe code on these forums and I think I've read somewhere here that it's evil, however I can't really figure out why. I'm now experimenting with directx in my tool and unsafe code is actually helping me a lot. So far the only thing I have found against using it is harder debugging, however I don't know what exactly it does under the hood.
For example this:
Code:
fixed ( Line* pBuffer = lines )
device.DrawPrimitiveUP( D3DPrimitiveType.LINELIST, lines.Length, pBuffer, sizeof( Line ) / 2 );
looks much simpler than it's safe alternative wich would probably look like this:
Code:
int size = Marshal.SizeOf( typeof( Line ) );
IntPtr pMemory = Marshal.AllocHGlobal( size * lines.Length );
try {
for ( int i = 0; i < lines.Length; i++ )
Marshal.StructureToPtr( lines[ i ], pMemory.Offset( i * size ), false );
device.DrawPrimitiveUP( D3DPrimitiveType.LINELIST, lines.Length, pMemory, size / 2 );
}
finally {
Marshal.FreeHGlobal( pMemory );
}
this is probably wrong forum, but I'm afraid nobody reads the c# section so sorry about that