25 Replies
I'm very confused. Why is it saying it's not a variable?
Is it because it's not mutable somehow?
Does it compile regardless of the error?
No
you cant do that witha struct of vector2 i think
it's immutable?
i cant explain in fancy words rn, but its because you use it with property
This error occurs because value types are copied on assignment. When you retrieve a value type from a property or indexer, you are getting a copy of the object, not a reference to the object itself.
there you got the fancy answer^^
This is correct. When you do
p.Position
, you get a copy of the value in p.Position
, not the same instance. So incrementing any properties on that copy won't increment the original thing in p
What you probably wanted to do was: p.Position = p.Position with { X = p.Position.X + 1 };
interesting
is this only an issue with
struct
? Like if I defined my own Vector2
class
would I be able to do it the other wayYes, it is specific to structs
Yes
If you're familiar with C++, you can think of a class variable (whether that's a property, local, parameter) as
Class&
While a struct variable is just Class
Why was it done like that? If I may ask
Not really sure what you mean by asking that, tbh.
class
fundamentally means "on the heap" in C#
struct
fundamentally means "in its container"Ahhh
All the other semantics fall out from that
Wow after years of C#, still learning more (though it feels weird that I didn't know this LOL)
Okay thank you ^-^ @Marvin Thanks to you too!
One of the things I think we succeed at is making sure most people don't need to know this 🙂
Keep up the awesome work with C#, I'm obssessed with it lmao
my fav lang by far
Thanks 🙂
Took me way too long at the beginning to actually know what dependency injection does under the hood, until I actually looked at how it is done, both the expression way and source gen
Never thought about it
I cannot claim any credit for di libraries 😄
Ah okee
It works with all structs