使用不安全的代码 [英] Using Unsafe code

查看:131
本文介绍了使用不安全的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯在我的函数中使用不安全的代码...



i m using unsafe code in my function like this...

public unsafe void swap(int* p_x, int* p_y)
        {
            int temp = *p_x;
            *p_x = *p_y;
            *p_y = temp;
        }







但是当我编译我的应用程序时我收到错误

只有在使用/ unsafe进行编译时才会出现不安全的代码



请帮助我




but when I compile my application i get error
"Unsafe code may only appear if compiling with /unsafe"

please help me

推荐答案

为什么不避免不安全的代码?你可以通过引用传递值类型,你只需要明确。

Why not just avoid the unsafe code? you can after all pass the value type by reference, you just have to be explicit.
public void swap(ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}





来自JSOP:这实际上是更正确的答案。除非使用不安全的代码有明显的优势,否则应尽可能避免使用它。这个答案从我那里得到了5分。



From JSOP: This is actually the more correct answer. Unless there's a clear advantage to using unsafe code, you should avoid it whenever possible. This answer gets a 5 from me.






我同意解决方案2.

尽可能避免使用不安全的代码。



当你还想使用不安全的代码时:



错误你得到的意思是:

编译/不安全。



在Visual Studio中

项目属性=> Build =>检查允许不安全代码CheckBox。



祝你好运,

Stephan
Hi,

I agree with Solution 2.
Avoid unsafe code whenever possible.

When you still want to use unsafe code:

The error you get means:
"Compile with /unsafe".

In your Visual Studio under
Project Properties => Build => Check the "Allow unsafe code" CheckBox.

Best regards,
Stephan


这篇关于使用不安全的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆