3 ms·
Yes, but for a reference types that value IS a reference, so if you change a complex type, that change will persist after return to the calling code.
by graycrow 8y ago
Yes, but for a reference types that value IS a reference, so if you change a complex type, that change will persist after return to the calling code.
- aurelwu 8y agostatic void Change(int[] myArray) { myArray = new int[5] {3, 1, 2}; } this will not change the array which was passed in and is a purely local change in C#. static void Change(ref int[] myArray) on the other side would change the array which was passed in.
- city41 8y agoBut it’s still pass by value. Passing by ref allows swapping out the entire object, which is not possible in a language such as JavaScript.
- sciurus 8y agoRight, but this is the same way Python works. Jumping back to the original argument in the post, the author claimed that Python was "going out of it's way to be different" by passing objects by reference value. C# provides a counterxample to that claim.
- porpoisely 8y agoYou are correct that changes to the object will be reflected in the calling code. But that's the nature of the reference, not whether it is called by value or called by reference. Whether you pass a reference by value or by reference, the changes to the object will be reflected in the calling code. And as I noted, it's all passed by value in C# unless you use modifiers ( ref or out ). The difference between "pass by value" and "pass by reference" for reference variables is how the variables themselves are handled. For example, if you pass a reference "by reference" to a function and set it to null, then the reference variable in the calling code is also set to null and will cause null ref exception if you try to access the object. Whereas if you pass a reference "by value", if you set the variable to null, the calling code variable isn't affected. Pass by value and pass by reference is not about objects or types really, it's about variables.