How do I know whether variable is pass by value or by reference?

For example transform seems always pass by reference, so we usually use code like this:


When the object move, myTr also change.

But in other cases, Vector, for example, seems pass by value.


The variable pos likely doesn’t change as object move. It’s a little confused sometimes.

Structs (Vector2, Vector3, Color, etc.) and primitive types (int, float, byte, etc.) are by value, everything else is by reference. The docs say whether something is a struct.

In C#, Value Types (struct,enum) are passed by value and Reference Types (class,interface,delegate) are passed by reference. You can pass a Value Type as reference using the ref keyword.

See MSDN Value Types and Reference Types.

Structs are passed as value and Classes are passed as reference.

If you want to pass a Struct as a reference, you can define the parameter in the function with ‘ref’ modifier

like for example:

void DoSomething(ref Vector3 vec)
    vec.y = 0;