Why are objects automatically passed by reference?

你说的曾经没有我的故事 提交于 2019-11-30 18:55:48

Why are objects automatically passed by reference?

They're not.

Is there any particular benefit from forcing the cloning process for them instead of treating objects more like int, double, boolean, etc. in these cases?

There's no "cloning process" for reference types, only for value types.

I think you're confusing different concepts:

  • value types vs. reference types

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

    For reference types (such as object, string, classes (not structs) etc), the value of the variable is a reference to the object. Assigning the variable to another (or passing it as a parameter by value) creates a copy of the reference, so it still refers to the same object instance.

  • passing parameters by value vs. by reference

    Passing parameters by value means that you pass a copy of the value. Depending on whether it's a value type or reference types, that means a copy of the object itself, or a copy of the reference. If the callee modifies members of a value type passed as a parameter, the caller won't see the changes, since the callee is working on a copy. On the other hand, if the callee modifies members of a reference type passed as a parameter, the caller will see the changes, because the callee and caller both have a reference to the same object instance.

    Passing parameters by reference means that you pass a reference to a variable (which may be a variable of value type or reference type). The value is not copied: it is shared between the caller and the callee. So any change made by the callee (including assignment of a new value to the parameter) will be seen by the caller.

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

I suggest you read Jon Skeet's article Parameter passing in C# for a better explanation.

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

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

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

I'll rephrase your question: Why do we need classes? Can't we just have only structs?

Not all objects are safe to copy. You can't logically copy a FileStream or a Button for example. These objects have identity and you want all code to refer to the one and only object.

A variable, parameter, or field of a class or interface type (collectively, "reference types") does not hold a class object; it holds an object identifier. Likewise, array of a reference type doesn't hold objects; it holds object identifiers.

Even though objects in .NET do not have any human-readable identifier associated with them, it may be helpful to reason about them as though they do: if at least some number (e.g. 592) of objects are created during the course of a program's execution, exactly one object will be the 592nd one created; once the 592nd object is created, no other object will ever be the 592nd. There's no way to find out which object is the 592nd, but if a variable which holds a reference to the 592nd object is passed as a non-ref parameter to some method, it will continue to hold a reference to the 592nd object when the method returns. If object #592 is a reference to an instance of Car which is colored red, a local variable myCar holds "Object ID #592", and one calls a method PaintCar(myCar);, then that method will receive Object #592". If that method paints the car blue, then when it returns, myCar will hold "Object #592", which will identify a blue car.

ok ill edit this: i stand corrected from the below article

humm i guess im not good at explaining things.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!