Does Vector3 B = new Vector3(X,Y,Z) + A create B as a reference to A?
In other words, if I use the new keyword to declare a variable, does it automatically apply to the entire line or just the following term? How do I know if I'm creating a reference or not?
(assume, obviously, that A is also a vector 3 or other object that can be cast to vector 3).
Answer by Naphier · May 13, 2016 at 09:11 AM
"new" only applies to the constructor following it. Vector3 is a struct and thus is a bad example for reference because it is always a copy.
So
Vector3 a = new Vector3(1,2,3);
Vector3 b = a;
b.x = 0;
Will still result in a.x = 1 If you just say
Vector3 c;
Then that results in the default value of (0,0,0). c is a new Vector3.
If it were not a struct then it would be a reference. For example, say you have a class Foo.
Foo a = new Foo();
Foo b = a;
b.field = 1;
a.field will also = 1. b is a reference to a.
This may provide further explanation: http://www.albahari.com/valuevsreftypes.aspx
If
Vector3 is a struct and thus is a bad example for reference because it is always a copy
Then why do I need to use the "new" keyword at all when using Vector3s?
Look this up on C# forums. Lots more info about how/why C# does things a certain way. I think I even wrote about that exact thing on StackOverflow.
You don't *have*to. You could also do Vector3 a; a.x = 1; a.y = 2; a.z = 3; The struct constructor is provided because it is convenient. $$anonymous$$ost structs will have constructors. All constructors are called with new.
The "new" keyword in C# has different sematics than one might be used to from languages like C++. In C++ the "new" operator always allocates memory on the heap. In C# the new keyword doesn't necessarily do that. "new" is required whenever you want to call a constructor.
When you construct a value type (structs) with "new" no heap is required since value type variables always have their required size. So a local Vector3 variable, which lives on the stack, is "created" at the moment you enter the method by for$$anonymous$$g a stackframe.
When you construct a reference type with "new" it automatically reserves memory on the heap for that new instance. Reference types can't live on the stack, they are always on the managed heap. References can only reference heap memory and never stack memory.
Value types (structs as well as primitive types like int, float, ...) can also live on the heap, but you never have to create memory manually for those. The process of placing a value type object on the managed heap happens implicitly. This process is called boxing. Boxing happens whenever you "convert" / assign a value type into the general base class "object".
int i = 5; // lives on the stack
object o = null; // The variable "o" also lives on the stack but as a reference type the actual memory on the stack only contains the reference / address of an object on the heap
o = i; // here i get boxed automatically. So memory on the heap is allocated where the value of i will be stored and o will reference that memory.
i = (int)o; // This is called unboxing. Here the value which is stored in heap memory is copied back into an actual int variable which lives on the stack.
As soon as the "o" variable goes out of extent the memory that was used for the boxed variable can be collected by the GC.
Like @Naphier said you usually don't have to call the constructor of a value type manually. For valuetypes you actually can't define a custom parameter-less constructor. That is always provided by the system.
$$anonymous$$ember variables of a class are initialized automatically when the class is created. The field initializers are called before the actual constructor of the class. If no field initializer is provided than the variables get initialized with their default value which is usually "0" for numerical primitive types and "null" for all reference types.
Local variables on the other hand aren't initialized automatically and need to be initialized expliciitly. For structs it's enough to set all of the member variables of the struct to something. However structs that have private variables must be initialized with a constructor since you can't set private variables from the outside.