From the documentation:
Unlike classes, structs can be instantiated without using a new operator.
So why am I getting this error:
Use of unassigned local variable ‘x’
When I try to do this?
Vec2 x;
x.X = det * (a22 * b.X - a12 * b.Y);
x.Y = det * (a11 * b.Y - a21 * b.X);
Where Vec2 x is a struct?
Well, are X and Y properties (rather than fields)? If so, that’s the problem. Until all the fields within
xare definitely assigned, you can’t call any methods or properties.For instance:
Is
Vec2your own type? Do you have access to the fields involved, or only the properties?If it’s your own type, I would strongly urge you to try to stick to immutable structs. I know managed DirectX has some mutable structs for getting as close to optimal performance as possible, but that’s at the cost of strange situations like this – and much worse, to be honest.
I would personally give the struct a constructor taking X and Y: