C
C#3y ago
Gladiator

Equals for structs [Answered]

Is it standard to implement GetHashCode and == and Equals for structs even if all fields should be used? because the default one is using reflection to get fields
4 Replies
Pheubel
Pheubel3y ago
it depends a bit. this article goes in depth about using it: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type but for just value holders you don't compare it is not that needed, if it is used for comparisons, then it makes sense to create an override that does it properly.
How to define value equality for a class or struct - C# Programming...
Learn how to define value equality for a class or struct. See code examples and view available resources.
mtreit
mtreit3y ago
Consider just using a record struct and get the relevant implementation for free
Gladiator
GladiatorOP3y ago
I cannot use it because it has been added in C#8.0 My env: Unity and it does not support record
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server