Why do I get the error "Unsafe code may only appear if compiling with /unsafe"?

C#Visual Studio-2008Windows CeUnsafe

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

Here is a screenshot:

Unsafe screenshot

ََََََََ

Solution 3 - C#

Probably because you're using unsafe code.

Are you doing something with pointers or unmanaged assemblies somewhere?

Solution 4 - C#

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

Solution 5 - C#

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

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGoldView Question on Stackoverflow
Solution 1 - C#GuffaView Answer on Stackoverflow
Solution 2 - C#Manoj AttalView Answer on Stackoverflow
Solution 3 - C#Gerrie SchenckView Answer on Stackoverflow
Solution 4 - C#RichardView Answer on Stackoverflow
Solution 5 - C#dipankar ranjan baisyaView Answer on Stackoverflow
Solution 6 - C#Tobias BrohlView Answer on Stackoverflow