C# 为什么会出现错误“不安全代码可能仅在使用 /unsafe 编译时出现”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2026410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get the error "Unsafe code may only appear if compiling with /unsafe"?
提问by Gold
Why do I get the following error?
为什么会出现以下错误?
Unsafe code may only appear if compiling with /unsafe"?
只有在使用 /unsafe 编译时才会出现不安全的代码?
I work in C# and Visual Studio 2008 for programming on Windows CE.
我使用 C# 和 Visual Studio 2008 在 Windows CE 上进行编程。
采纳答案by Guffa
To use unsafe code blocks, the project has to be compiled with the /unsafe switch on.
要使用不安全的代码块,必须在打开 /unsafe 开关的情况下编译项目。
Open the properties for the project, go to the Build
tab and check the Allow unsafe code
checkbox.
打开项目的属性,转到Build
选项卡并选中Allow unsafe code
复选框。
回答by Gerrie Schenck
Probably because you're using unsafe code.
可能是因为您使用了不安全的代码。
Are you doing something with pointers or unmanaged assemblies somewhere?
您是否在某处使用指针或非托管程序集进行操作?
回答by Richard
Search your code for unsafe
blocks or statements. These are only valid is compiled with /unsafe
.
在代码中搜索unsafe
块或语句。这些仅在编译时有效/unsafe
。
回答by dipankar ranjan baisya
To use unsafe code blocks, open the properties for the project, go to the Buildtab and check the Allow unsafe codecheckbox, 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
回答by Manoj Attal
Here is a screenshot:
这是一个屏幕截图:
????????
?????????