1

I've been told that when you pass an Object to a method, it's passed "by value". I made a little test to examine it:

Point p = new Point(1, 1);
Circle c = new Circle(p);
p.x = 999;
Console.WriteLine(c.p.x);

the code above prints "999", but I thought the object is copied to the method
I've been told that if you're not using "ref" (or "out") the method get the value
of the object.

can someone make it clear to me?
thanks,
socksocket

socksocket
  • 4,271
  • 11
  • 45
  • 70

4 Answers4

3

Assuming Point is declared as class, not p itself is copied, the reference to p is copied. So it's still pass by value. You pass the value of a reference.

When saying Point p = new Point(1, 1); (and if point is a reference type), one might think it is a variable containing a Point, but in fact it is a variable containing a reference to a Point that is stored somewhere else.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • @Botz3000 - Can you bring an example of "pass by reference"? – Saul Jun 04 '12 at 13:34
  • @Saul Arguments can be passed by reference by using the `ref` or `out` keywords. It's rarely needed though. – Botz3000 Jun 04 '12 at 13:41
  • @Botz3000 - Okay. How would `Circle c = new Circle(out p);` be different? – Saul Jun 04 '12 at 14:18
  • @Saul You would be able to modify the value of the variable p. In this context this means, inside the Circle constructor, you could assign p a completely new object. Without the `out` modifier, the original variable p would still point to the same object. – Botz3000 Jun 04 '12 at 14:52
1

C# is pass-by-value - the reference value is passed in the normal case, that is. ( which means that it is a new reference to the same object)

manojlds
  • 290,304
  • 63
  • 469
  • 417
1

Point is structure, so passed by value.

Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
1

In .NET, there are two categories of types, reference types and value types.

Structs are value types and classes are reference types.

The general different is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.

A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.

A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.

This has one benefit, to begin with:

  • value types always contains a value
  • reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment

Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:

  • copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
  • copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places

When you declare variables or fields, here's how the two types differ:

  • variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
  • class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.

See MSDN and this tutorial is similar to your Circle / Point / Axises Example.

Ahmed Ghoneim
  • 6,834
  • 9
  • 49
  • 79