Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any point to avoiding to use "unsafe" in Mono?

Tags:

c#

mono

unsafe

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?

like image 817
cib Avatar asked May 23 '11 17:05

cib


1 Answers

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).

like image 181
skolima Avatar answered Nov 03 '22 07:11

skolima