What is the actual function of the C# project setting “Allow unsafe code”

情到浓时终转凉″ 提交于 2019-12-22 05:51:16

问题


I was wondering if the C# project setting "Allow unsafe code" applies only to unsafe C# code in the project itself, or is it necessary to set this option when linking in a native C++ DLL? What about linking in a managed DLL that itself links to a native DLL? What does this option really do, under the hood?


回答1:


It has to do with the "unsafe" keyword in C#. "unsafe" turns off all the checks that would normally happen and allow you to directly access the memory. it doesn't refer to calling native C++ DLL's or interfaces.




回答2:


It allows you to use the "unsafe" block.

unsafe(...)
{
}



回答3:


This just relates to the use of unsafe blocks (where pointers can be used). It does not govern P/Invoke.




回答4:


Its necessary to use the unsafe { } context. It used to be required to use sizeof() but in later versions that's no longer true.

You don't need to allow unsafe code if you are externing to another DLL written in another language like C.



来源:https://stackoverflow.com/questions/233759/what-is-the-actual-function-of-the-c-sharp-project-setting-allow-unsafe-code

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