你应该在C#中使用指针(不安全代码)吗? [英] Should you use pointers (unsafe code) in C#?

查看:500
本文介绍了你应该在C#中使用指针(不安全代码)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你应该在C#代码中使用指针吗?有什么好处?

Should you use pointers in your C# code? What are the benefits? Is it recommend by The Man (Microsoft)?

推荐答案

从The Man自己:

From "The Man" himself:

在C#中很少需要使用指针,但在某些情况下需要使用它们。例如,使用不安全的上下文来允许指针是由以下情况所保证的:

The use of pointers is rarely required in C#, but there are some situations that require them. As examples, using an unsafe context to allow pointers is warranted by the following cases:


  • 处理磁盘上的现有结构

  • 高级COM或平台调用涉及具有指针的结构的场景

  • 性能关键代码

不鼓励在其他情况下使用不安全的上下文。

The use of unsafe context in other situations is discouraged.

具体来说,不应使用不安全的上下文尝试在C#中编写C代码。

注意:

使用不安全的上下文写入的代码无法验证是否安全,因此只有在完全信任代码时才会执行。换句话说,不安全的代码不能在不受信任的环境中执行。例如,您无法直接从Internet运行不安全代码。

Code written using an unsafe context cannot be verified to be safe, so it will be executed only when the code is fully trusted. In other words, unsafe code cannot be executed in an untrusted environment. For example, you cannot run unsafe code directly from the Internet.

参考

这篇关于你应该在C#中使用指针(不安全代码)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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