Does it make sense to avoid using "unsafe" in Mono?

Recently, I came across the fact that I need to specify the parameter "unsafe" when working with some concepts in C # / Mono. I not only have to specify this parameter in my code, but I must also indicate it when compiling, which makes me feel "this code is inherently unsafe and dangerous to use." The strangest thing here is that DllImport does not need an unsafe flag, so I can create segfaults all I want in my "safe code". Is there any reason to avoid unsafe code that I forgot?

+5
source share
1 answer

DllImport is a well-known concept for most C # developers. This also leads to a relatively small area where segfault can occur. With code, unsafeyou can make random crashes much easier in places not related to the call unsafe(by manipulating memory areas outside of what you think you were touching).

This is very similar to compiler warnings - some developers do not pay attention to them, some turn "Treat warnings as errors" to "Everything." Guess which results in the code are easier to simplify?

In addition, DllImport itself is not a good idea if it can be avoided (due to possible break errors / missed errors / portability, etc.).

+7
source

All Articles