They're more limited. You can say ++ on a pointer, but not on a ref
or out
.
It's a safe bet that they are internally just pointers, because the stack doesn't get moved and C# is carefully organised so that ref
and out
always refer to an active region of the stack.
If you ever play with interop in unsafe code, you will find that ref
is very closely related to pointers. For example, if a COM interface is declared like this:
HRESULT Write(BYTE *pBuffer, UINT size);
The interop assembly will turn it into this:
void Write(ref byte pBuffer, uint size);
And you can do this to call it (I believe the COM interop stuff takes care of pinning the array):
byte[] b = new byte[1000];
obj.Write(ref b[0], b.Length);
In other words, ref
to the first byte gets you access to all of it; it's apparently a pointer to the first byte.