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

让人想犯罪 __ 提交于 2019-11-27 09:36:50

问题


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.


回答1:


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.




回答2:


Here is a screenshot:

ََََََََ




回答3:


Probably because you're using unsafe code.

Are you doing something with pointers or unmanaged assemblies somewhere?




回答4:


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




回答5:


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



回答6:


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.



来源:https://stackoverflow.com/questions/2026410/why-do-i-get-the-error-unsafe-code-may-only-appear-if-compiling-with-unsafe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!