c# - 不安全关键字仅检查参数?
问题描述
我是 C# 新手,仍在努力理解 unsafe 的使用。假设我们有以下代码:
static void Main(string[] args)
{
unsafe
{
int myInt = 10;
SquareIntPointer(&myInt ); //OK here, in unsafe context
}
int myInt2 = 5;
SquareIntPointer(&myInt2); // compile error, not in unsafe context
}
static unsafe void SquareIntPointer(int* myIntPointer)
{
// Square the value just for a test.
*myIntPointer *= *myIntPointer;
}
但如果我将代码更改为:
static void Main(string[] args)
{
int myInt2 = 5;
SquareIntPointer(); // OK here, but why?
}
static unsafe void SquareIntPointer()
{
int random = 10;
int* myIntPointer = &random;
*myIntPointer *= *myIntPointer;
}
所以我们这里没有任何编译错误,但是SquareIntPointer方法不是还在处理方法体中的指针类型吗?并不意味着只检查参数以查看它是否是指针类型?那会有点愚蠢。
解决方案
推荐阅读
- perl - 查找哈希值的平均值 Perl
- ms-access - microsoft access中的代码组合(yyxxxx格式)
- typescript - 防止接口从标准库类型合并
- python - 在numpy中为train_test_split输入参数时,我是使用原始x数据点矩阵还是设计矩阵?
- c# - 碰撞检测和矩形
- angular - 尝试通过 Angular 的 HttpClient 返回的 RxJS 数据进行过滤,该 HttpClient 从数据库表返回数据
- javascript - 无法在 ajax + Rails 中获取评论
- linux - 程序不适用于 crontab(路径问题)
- javascript - 在普通 JavaScript 中从 Node.js 导入类
- asp.net - 替换引导代码后导航栏未固定在顶部