C
C#•3mo ago
Ab

Why am I unable to increment this property?

No description
25 Replies
Ab
AbOP•3mo ago
No description
Ab
AbOP•3mo ago
I'm very confused. Why is it saying it's not a variable? Is it because it's not mutable somehow?
Angius
Angius•3mo ago
Does it compile regardless of the error?
Ab
AbOP•3mo ago
No
Marvin
Marvin•3mo ago
you cant do that witha struct of vector2 i think
Ab
AbOP•3mo ago
it's immutable?
Marvin
Marvin•3mo ago
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^^
333fred
333fred•3mo ago
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 };
Ab
AbOP•3mo ago
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 way
333fred
333fred•3mo ago
Yes, it is specific to structs
Marvin
Marvin•3mo ago
Yes
333fred
333fred•3mo ago
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
Ab
AbOP•3mo ago
Why was it done like that? If I may ask
333fred
333fred•3mo ago
Not really sure what you mean by asking that, tbh. class fundamentally means "on the heap" in C# struct fundamentally means "in its container"
Ab
AbOP•3mo ago
Ahhh
333fred
333fred•3mo ago
All the other semantics fall out from that
Ab
AbOP•3mo ago
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!
333fred
333fred•3mo ago
One of the things I think we succeed at is making sure most people don't need to know this 🙂
Ab
AbOP•3mo ago
Keep up the awesome work with C#, I'm obssessed with it lmao my fav lang by far
333fred
333fred•3mo ago
Thanks 🙂
Marvin
Marvin•3mo ago
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
333fred
333fred•3mo ago
I cannot claim any credit for di libraries 😄
Marvin
Marvin•3mo ago
Ah okee
333fred
333fred•3mo ago
It works with all structs
Want results from more Discord servers?
Add your server