Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ref and out in C# the same a pointers in C++?

I just made a Swap routine in C# like this:

static void Swap(ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}

It does the same thing that this C++ code does:

void swap(int *d1, int *d2)
{
    int temp=*d1;
    *d1=*d2;
    *d2=temp;
}

So are the ref and out keywords like pointers for C# without using unsafe code?

like image 838
Bob Dylan Avatar asked Jul 22 '09 18:07

Bob Dylan


6 Answers

They're more limited. You can say ++ on a pointer, but not on a ref or out.


EDIT Some confusion in the comments, so to be absolutely clear: the point here is to compare with the capabilities of pointers. You can't perform the same operation as ptr++ on a ref/out, i.e. make it address an adjacent location in memory. It's true (but irrelevant here) that you can perform the equivalent of (*ptr)++, but that would be to compare it with the capabilities of values, not pointers.


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.


EDIT To be absolutely clear again (if it wasn't already clear from the example below), the point here is not that ref/out can only point to the stack. It's that when it points to the stack, it is guaranteed by the language rules not to become a dangling pointer. This guarantee is necessary (and relevant/interesting here) because the stack just discards information in accordance with method call exits, with no checks to ensure that any referrers still exist.

Conversely when ref/out refers to objects in the GC heap it's no surprise that those objects are able to be kept alive as long as necessary: the GC heap is designed precisely for the purpose of retaining objects for any length of time required by their referrers, and provides pinning (see example below) to support situations where the object must not be moved by GC compacting.


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.

like image 130
Daniel Earwicker Avatar answered Oct 13 '22 18:10

Daniel Earwicker


Reference parameters in C# can be used to replace one use of pointers, yes. But not all.

Another common use for pointers is as a means for iterating over an array. Out/ref parameters can not do that, so no, they are not "the same as pointers".

like image 31
jalf Avatar answered Oct 13 '22 19:10

jalf


ref and out are only used with function arguments to signify that the argument is to be passed by reference instead of value. In this sense, yes, they are somewhat like pointers in C++ (more like references actually). Read more about it in this article.

like image 44
Saulius Valatka Avatar answered Oct 13 '22 18:10

Saulius Valatka


Actually, I'd compare them to C++ references rather than pointers. Pointers, in C++ and C, are a more general concept, and references will do what you want.

All of these are undoubtedly pointers under the covers, of course.

like image 41
David Thornley Avatar answered Oct 13 '22 18:10

David Thornley


The nice thing about using out is that you're guaranteed that the item will be assigned a value -- you will get a compile error if not.

like image 2
bufferz Avatar answered Oct 13 '22 18:10

bufferz


While comparisons are in the eye of the beholder...I say no. 'ref' changes the calling convention but not the type of the parameters. In your C++ example, d1 and d2 are of type int*. In C# they are still Int32's, they just happen to be passed by reference instead of by value.

By the way, your C++ code doesn't really swap its inputs in the traditional sense. Generalizing it like so:

template<typename T>
void swap(T *d1, T *d2)
{
    T temp = *d1;
    *d1 = *d2;
    *d2 = temp;
}

...won't work unless all types T have copy constructors, and even then will be much more inefficient than swapping pointers.

like image 1
Richard Berg Avatar answered Oct 13 '22 18:10

Richard Berg