Why do I get the error “Unsafe code may only appea

2019-01-21 02:59发布

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.

6条回答
我想做一个坏孩纸
2楼-- · 2019-01-21 03:30

Probably because you're using unsafe code.

Are you doing something with pointers or unmanaged assemblies somewhere?

查看更多
放荡不羁爱自由
3楼-- · 2019-01-21 03:30

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

查看更多
别忘想泡老子
4楼-- · 2019-01-21 03:32

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.

查看更多
5楼-- · 2019-01-21 03:32

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

查看更多
做个烂人
6楼-- · 2019-01-21 03:34

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
查看更多
混吃等死
7楼-- · 2019-01-21 03:38

Here is a screenshot:

Unsafe screenshot

ََََََََ

查看更多
登录 后发表回答