157

Why do I get the following error?

Unsafe code may only appear if compiling with /unsafe"?

I work in C# and Visual Studio 2008 for programming on Windows CE.

jacknad
  • 13,483
  • 40
  • 124
  • 194
Gold
  • 60,526
  • 100
  • 215
  • 315
  • 1
    See http://msdn.microsoft.com/en-us/library/ezb5hwx9%28VS.80%29.aspx and http://msdn.microsoft.com/en-us/library/t2yzs44b%28VS.80%29.aspx – Dirk Vollmar Jan 08 '10 at 09:07
  • Possible duplicate of [I can seem to get msbuild to build unsafe code blocks](https://stackoverflow.com/questions/38992374/i-can-seem-to-get-msbuild-to-build-unsafe-code-blocks) – stack247 Jun 09 '18 at 05:38

7 Answers7

297

To use unsafe code blocks, the project has to be compiled with the /unsafe switch on.

Open the properties for the project, go to the Build tab and check the Allow unsafe code checkbox.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 4
    I must say, even though that does enable the build to compile, it still doesn't allow it to get published to the web :/ – Nick Sep 17 '13 at 13:30
  • 5
    @Nick: Yes, if you publish code to be compiled dynamically, then the project settings doesn't apply. See http://stackoverflow.com/questions/16567197/asp-net-publish-web-application-with-unsafe-code – Guffa Sep 17 '13 at 14:41
  • 14
    Note that the settings might differ between Debug and Release compile. This just cost me 20 minutes of my life. – LosManos Dec 02 '15 at 15:07
  • You may have to change the build configuration from Debug to Release for different type of builds – SZT Sep 17 '20 at 05:03
135

Here is a screenshot:

Unsafe screenshot

ََََََََ

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manoj Attal
  • 2,806
  • 3
  • 28
  • 31
  • 11
    Important: Also be aware that this screenshot is for "Configuration: Active (Debug)". You'll probably also need to change it for "Release", since that's most likely what you're publishing. – Doug S May 08 '17 at 18:42
4

Probably because you're using unsafe code.

Are you doing something with pointers or unmanaged assemblies somewhere?

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
4

Search your code for unsafe blocks or statements. These are only valid is compiled with /unsafe.

Richard
  • 106,783
  • 21
  • 203
  • 265
4

To use unsafe code blocks, open the properties for the project, go to the Build tab and check the Allow unsafe code checkbox, then compile and run.

class myclass
{
     public static void Main(string[] args)
     {
         unsafe
         {
             int iData = 10;
             int* pData = &iData;
             Console.WriteLine("Data is " + iData);
             Console.WriteLine("Address is " + (int)pData);
         }
     }
}

Output:

Data is 10
Address is 1831848
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

For everybody who uses Rider you have to select your project>Right Click>Properties>Configurations Then select Debug and Release and check "Allow unsafe code" for both.Screenshot

Tobias Brohl
  • 479
  • 6
  • 25
3

Can also add AllowUnsafeBlocks tag to PropertyGroup directly in .csproj file

<PropertyGroup>
    <TargetFrameworks>netcoreapp3.1; net472</TargetFrameworks>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Manish
  • 1,452
  • 15
  • 25