C# 引用和指针之间有什么区别? [英] What is the difference between a C# Reference and a Pointer?

查看:59
本文介绍了C# 引用和指针之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白 C# 引用和指针之间的区别.他们都指向记忆中的一个地方,不是吗?我能弄清楚的唯一区别是指针没有那么聪明,不能指向堆上的任何东西,免于垃圾收集,并且只能引用结构或基类型.

I do not quite understand the difference between a C# reference and a pointer. They both point to a place in memory don't they? The only difference I can figure out is that pointers are not as clever, cannot point to anything on the heap, are exempt from garbage collection, and can only reference structs or base types.

我问的一个原因是,人们认为人们需要很好地理解指针(我猜是从 C 语言)才能成为一名优秀的程序员.很多学习高级语言的人都忽略了这一点,因此就有了这个弱点.

One of the reasons I ask is that there is a perception that people need to understand pointers well (from C, I guess) to be a good programmer. A lot of people who learn higher level languages miss this out and therefore have this weakness.

我只是不明白指针有什么复杂的地方?它基本上只是对内存中某个位置的引用,不是吗?它可以返回它的位置并直接与该位置的对象进行交互吗?

I just don't get what is so complex about a pointer? It is basically just a reference to a place in memory is it not? It can return its location and interact with the object in that location directly?

我是否错过了一个重要的点?

Have I missed a massive point?

推荐答案

C# 引用可以,并且会被垃圾收集器重定位,但普通指针是静态的.这就是为什么我们在获取指向数组元素的指针时使用 fixed 关键字来防止它被移动.

C# references can, and will be relocated by garbage collector but normal pointers are static. This is why we use fixed keyword when acquiring a pointer to an array element, to prevent it from getting moved.

从概念上讲,是的.它们或多或少相同.

Conceptually, yes. They are more or less the same.

这篇关于C# 引用和指针之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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