9

For some reason, (I am new to C# and know java and c++) C# keeps copying objects when I want to pass by value. I have an arraylist of a Vector2 class, and whenever I want to increment a value, I have to do this:

Vector2 d = (Vector2) myObjects[i];
d.Y++;
myObjects [i] = d;

I want to be able to do this:

Vector2 d = (Vector2) myObjects[i];
d.Y++;

and be done. I searched the web and surprisingly no answers. BTW the vector is a struct.

10
  • 4
    Show the code for vector. Is it a struct? Commented Oct 19, 2013 at 1:16
  • How about: ((Vector2)myObjects[i]).Y++; Commented Oct 19, 2013 at 1:18
  • @MikeChristensen I already tried that it gave me an error Commented Oct 19, 2013 at 1:19
  • Since Vector2 is struct then it is clear why - structs are value types in c#. Use ref keyword and you should be OK Commented Oct 19, 2013 at 1:20
  • possible duplicate of C# pass by value/ref? Commented Oct 19, 2013 at 1:22

2 Answers 2

10

In C#, instances of classes are passed as references, whereas instances of structs are passed by copy (by default).

The answer was just where it was supposed to be: http://msdn.microsoft.com/en-us/library/vstudio/ms173109.aspx

A class is a reference type. When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

Sign up to request clarification or add additional context in comments.

26 Comments

If you're using Unity3D, Vector2 is defined as a struct.
We had whole discussion on this yesterday. Object types are not passed by references - the reference passed by value.
Guys, this answer is not accurate. It explains how memory stack and heap works, not how variable passed. Totally incorrect
Pedantic point, but classes are not 'passed' by reference -- everything is passed by value by default. The ref keyword exists for a reason!
@NPFS3000 -- if you pass an instance of a class to a method, a copy of the reference is made (ergo it is passed by value). It's a subtle distinction but consider this: what would happen if you do this? void SomeMethod(ClassInstance a) { a = null; } -- Whatever variable was passed in to SomeMethod will not be re-assigned to null when the method returns.
|
4

You are experiencing one of the effects of value-types. Because it copies itself by value, rather than by reference when assigned to new variables or passed as an argument.

You can pass a struct or other value type by ref, using the ref keyword in your method signature, unfortunately you can't use it for treating a variable in the same stack frame as a reference (i.e. you can't just say ref int test = yourArray[0], but must make something like:

public void SomeMethod(ref Vector2 input) {
   // now you are modifying the original vector2
}
public void YourOriginalMethod() 
{        
    SomeMethod(yourArray[20]);
}

In response to the comment below, from http://msdn.microsoft.com/en-us/library/14akc2c7.aspx:

Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference.

2 Comments

as far as I know this will involve "boxing" which is something to avoid if possible.
In the case of C# at least when passing a struct by ref you aren't boxing it. It's just passing the reference to the location on the stack, which has a lifespan limited by its stack frame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.