Mar 14

References are not pointers in C#

Posted in C#      Comments Off on References are not pointers in C#

As you know, C# supports the concepts of references and value types. Most of the time, reference types tend to be presented as some sort of synonym for a memory address. I’ve seen it everywhere and I’ve used that analogy too when trying to help others to understand the difference between value types and reference types. Now, this kind of analogy tends to break when you start thinking about the memory model and on how GC works.

Say, if reference type is an address, then why do I need to pin it before saving a reference to that address? If you think about references without the address analogy, things start to make more sense…This is just an example of the kind of questions you might end up being asked when you use this sort of analogy to introduce reference types. Fortunately, we’ve got people like Eric Lippert who really  know their stuff and do have the time (and will!) to explain why that is not a good analogy.