I've recently come across the fact that I need to specify the "unsafe" option when working with certain concepts in C#/Mono. Not only do I have to specify the option in my code, I also have to specify it when compiling it, which invokes the feeling of "this code is inherently unsafe and risky to use". The odd thing here is that DllImport doesn't need the unsafe flag, so I can create segfaults all I want in my "safe code". Is there any reason to avoid unsafe code that I've overlooked?
DllImport is a well-known concept for most C# developers. It also results in a relatively small area where the segfault could occur. With the unsafe
code you can create random crashes much more easily, and in places not related to the unsafe
call (by manipulating memory regions outside of what you thought you were touching).
It's a lot like the compiler warnings - some developers pay no attention to them, some turn "Treat warnings as errors" to "All". Guess what results in code that is easier to maintain?
Also, DllImport itself is not a good idea if it can be avoided (because of potential breakage / missed errors / portability problems and so on).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With