I get an error when I compile this code:
using System; public struct Vector2 { public event EventHandler trigger; public float X; public float Y; public Vector2 func() { Vector2 vector; vector.X = 1; vector.Y = 2; return vector; // error CS0165: Use of unassigned local variable 'vector' } }
hi!
The compiler says: ‘Use of unassigned local variable ‘vector” and points to the return value. It looks to me that Vector2 become a reference type (without the event member it acts normally). What is happening?
In C# you still need to ‘new’ a struct to call a constructor unless you are initializing all the fields. You left EventHandler member ‘trigger’ unassigned.
Try either assigning to ‘trigger’ or using:
The new object is not allocated on the heap, it is still allocated on the functions stack.
To quote MSDN: