Why are objects automatically passed by reference?

I have a general question about a deep and shallow copy in the context of the pass-through concept and the pass-by-value concept of C #:

In C #, you need to explicitly create methods that accept pointers / references in order to be able to pass them to a method. However, at least the objects passed as parameters to the methods / constructors behave differently than the others. They seem to always be passed by reference unless additional cloning is performed, as described here: http://zetcode.com/lang/csharp/oopii/ .

Why are objects automatically passed by reference? Is there any particular benefit of forcing the cloning process for them instead of considering objects like int, double, boolean, etc. In these cases?

Here is a sample code that illustrates what I mean:

using System; public class Entry { public class MyColor { public int r = 0; public int g = 0; public int b = 0; public double a = 1; public MyColor (int r, int g, int b, double a) { this.r = r; this.g = g; this.b = b; this.a = a; } } public class A { public int id; public MyColor color; public MyColor hiddenColor; public A (int id, MyColor color) { this.id = id; this.color = color; } } static void Main(string[] args) { int id = 0; MyColor col = new MyColor(1, 2, 3, 1.0); A a1 = new A(id, col); A a2 = new A(id, col); a1.hiddenColor = col; a2.hiddenColor = col; a1.id = -999; id = 1; col.a = 0; Console.WriteLine(a1.id); Console.WriteLine(a2.id); Console.WriteLine(a1.color.a); Console.WriteLine(a2.color.a); Console.WriteLine(a1.hiddenColor.a); Console.WriteLine(a2.hiddenColor.a); } } 

This leads to:

 -999 0 0 0 0 

MyCol instances MyCol always passed by reference, and the rest of the arguments are passed by value. I would have to implement ICloneable in the MyColor and A classes. On the other hand, the isf'-operator is present in C #, which should be used to explicitly enable and execute pass-by-reference.

Suggestions are welcome!

+8
pass-by-reference pass-by-value c # clone shallow-copy
source share
5 answers

Why are objects automatically passed by reference?

This is not true.

Is there any particular advantage of the forced cloning process for them instead of handling more objects like int, double, boolean, etc. in these cases?

There is no cloning process for reference types, only for value types.

I think you are mixing different concepts:

  • value types compared to reference types

    For value types (such as primitive numeric types, enumerations, and structures such as DateTime ), the value of the variable is the object itself. Assigning another variable (or passing it as a parameter by value) creates a copy of the object.

    For reference types (for example, object , string , classes (not structures), etc.), the value of the variable is a reference to the object. Assigning a variable to a variable (or passing it as a parameter by value) creates a copy of the link, so it still refers to one instance of the object.

  • passing parameters by value by reference

    Passing parameters by value means that you are passing a copy of the value. Depending on whether it is a value type or reference types, it means a copy of the object itself or a copy of the link. If the callee changes the members of the type of the value passed as a parameter, the caller will not see the changes because the callee is working on the copy. On the other hand, if the caller changes the members of the referenced type passed as a parameter, the caller will see the changes, since the caller and the caller have a reference to the same instance of the object.

    Passing parameters by reference means that you are passing a reference to a variable (which can be a variable of type value or reference type). The value is not copied: it is shared between the caller and the called party. Thus, any change made by the called user (including the assignment of a new value to the parameter) will be visible to the caller.

    Unless otherwise specified (with the ref or out keywords), all parameters are passed by value, including reference types. It's just for reference types, the passed value is a reference, but it is still passed by value.

I suggest you read John Skeet's article Passing Parameters in C # for a better explanation.

+31
source share

All method arguments are passed by value unless you explicitly specify that they should be passed by reference using the ref or out keyword. This means that if you pass a variable to a method parameter, the contents of the variable will be copied and passed to the method.

If the variable is a value type, which basically means struct , then the variable contains the object and therefore the object is copied. If the variable is a reference type, which basically means class , then the variable contains a reference to the object so that the link is copied.

If you declare the parameter as ref or out , then a variable reference is created and passed to the method. If the variable contains an object, then a link to this object is created, and if the variable contains a link, a link to this link is created.

+2
source share

I will rephrase your question: why do we need classes? Can't we just have structures?

Not all objects are safe to copy. You cannot logically copy a FileStream or Button , for example. These objects have an identity, and you want all the code to refer to the same object.

+2
source share

A variable, parameter or field of a class or interface type (collectively, "reference types") does not contain a class object; it contains the identifier of the object. Similarly, an array of a reference type contains no objects; it contains object identifiers.

Despite the fact that objects in .NET do not have a human-readable identifier associated with them, it can be useful to reason about them as if they did: if at least a certain number (for example, 592) of objects are created during the course of the program’s execution, it’s exactly one object will be created 592nd; once 592nd is created, no other object will ever be 592nd. There is no way to know which object is 592nd, but if a variable that contains a reference to the 592nd object is passed as a non-ref parameter for some method, it will continue to refer to the 592nd object when the method returns. If object # 592 is a reference to a Car instance that is colored red, the local variable myCar contains "Object ID # 592", and one calls the PaintCar(myCar); method PaintCar(myCar); then this method will get Object #592" If this method paints a blue car, then when it returns myCar will hold" Object No. 592 "that identifies the blue car.

0
source share

ok, poorly edit this: I am standing corrected from the article below

humm, I think I'm not explaining things very well.

-2
source share

All Articles