error CS0227: Unsafe code may only appear if compiling with /unsafe

less than 1 minute read

This is the error message you get, when you build a project with some bit of unsafe code in it.

Simply you need to "Alt + Enter" on the project to see the properties and tick on "Allow unsafe code" on build tab. Then it compiles lovely in visual studio.

However, I came across the error message again in the build. with MSBuiild, I got the message. It's still ticked on the project's property page. What's wrong? ...

The culprit was build type. In VS, you usually set the build type to DEBUG. so the Allow unsafe code option is turned on against debug, not release. So, open the project file in a simple text editor. I used Sublime text 2. Make sure the option exists in both debug and release.

[sourcecode language="xml"]
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">