2

I always thought they were about the same thing but someone pointed me out in one of my answers that such ain't quite true.

Edit: Here is what I said and the comment I got. Edit2: What's the difference between C++'s:

public: void foo(int& bar);

and C#'s

public void foo(ref int bar){

} 
Community
  • 1
  • 1
Anzurio
  • 16,780
  • 3
  • 39
  • 49

4 Answers4

5

In C#, you have primitive types (ints, structs, etc.) and reference types (objects). These are built in to the language. In C++, you have to be explicit. Here is a set of equivalent ways of referring to objects in C# and C++, based on whether they are reference types or primitives, and whether or not you're using ref:

C# type                     | C++ Type
----------------------------+---------
Primitive type, with no ref | Normal parameter passing
Reference type, with no ref | Pointer (*)
Primitive type, with ref    | Reference (&)
Reference type, with ref    | Reference of a pointer (&*)
Smashery
  • 57,848
  • 30
  • 97
  • 128
2

C++'s reference '&' and C#'s ref are quite different.

The normal object passing of parameters in C# (ie. no 'ref' or 'out') is the equivalent of C++'s 'reference' passing. The callee gets a handle to the object and modifying fields on the object does modify the object originally passed by the caller. In C++ if you pass a normal object (no reference) the callee gets a copy of the object and modifying its fields does not affect the original object in the caller context. In C# if the callee assigns a new object to the argument then it will now operate on the new object, but the caller will not see this change, as the caller will still reference the old object in its context.

The 'ref' in C# means that the callee can actually change the handle it has received. So if he assign the handle to a new object, this change is visible in the caller context, the argument passed to the call will now 'point' to the new object. This is the equivalent of C++'s passing a reference to a pointer, so that the callee can change the address the pointer is pointing to. C#'s 'out' is just 'ref' with some semantic sugar around initialization, so all above applies to 'out'.

All this applies of course to how C# manipulate object parameters, not 'value types' (eg. ints).

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Does this mean that the 'ref' only applies to the variable passed (containing a handle), and _not_ to the object the handle points to? – xtofl May 29 '09 at 05:56
0

There is no "const ref" (which I guess would be called "in" as opposed to "out" in C#).

The C# compiler treats a "ref" as something that has to be valid on function call, and complains if you haven't initialized it, whereas it treats "out" as something that should not be initialized before, and must be assigned before return.

You can't "ref" a temporary or the return value of a property in C#.

Jon Watte
  • 6,579
  • 4
  • 53
  • 63
0

There are some differences -- C#'s ref is only used for arguments, and you can only pass initialized variables (use out instead to remove the latter limitation); C++'s reference syntax can be used a bit more freely (e.g. for local variables [must be initialized at definition time] and with no worries about initialization in the case of arguments)... but overall I'd say the differences are rather minor ones.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395