Zytan wrote:
>> Yes, they are. I think of them as pointers, because that's a familiar
>> idiom for me.
>>
>> Howver, as Arne pointed out, you can't do pointer arithmetic with
>> them, and, although you'll never notice it, unlike pointers references
>> can change at any moment as the Garbage Collector compacts the heap
>> and moves objects around. As I said, though, you won't notice, because
>> the reference will always refer to the same object instance, even if
>> it doesn't always point to the same place in memory.
>
> Bruce,
>
> Thanks for being so clear. I understand just what you're saying.
>
> They are like pointers, but you can't think of it as an actual memory
> addresses, since C# doesn't give you that kind of access, for good
> reason -- since the GC moves things around on you, as you say, and
> this means pointer arithmetic is no good. Also, since it's not really
> a pointer, you never need to use ptr-> or *ptr. you always use ptr.
> (ptr being an incorrect abbr. here, since it's not a pointer).
>
> So, it's like a pointer. But, it's really a reference.
>
> I just did a test and found that a class that has a private array, if
> it passes this out in a property, the caller has the 'pointer' and
> thus can change the contents of the private array. (I guess you'd
> have to make a copy to pass back in this case.)
Yes, reference types (objects) are never copied automatically. If you
want a copy you have to explicitly create one.
An alternative to creating a copy would be to return a wrapper object
that contains the array but only allows read access to it.
> So, in this case it
> acts like a pointer in C, so it's a nice way to think of them, but it
> is not a direct analogy. Got it.
>
> So, they are called 'references'? Is this the proper term?
>
Yes. It's the proper term.
--
Göran Andersson
_____
http://www.guffa.com