C
C#14mo ago
__dil__

❔ ✅ Struct equality

hi! I have a struct such as this one:
public readonly struct InternerId
{
private readonly int _id;

internal InternerId(int id)
{
_id = id;
}
}
public readonly struct InternerId
{
private readonly int _id;

internal InternerId(int id)
{
_id = id;
}
}
What's the correct way to implement equality between two InternerIds based on _id? I don't want equality with any other type except itself.
27 Replies
Thinker
Thinker14mo ago
Override bool Equals(object? other) and optionally implement IEquatable<InternerId> You could also use a record struct, which implements equality by default
__dil__
__dil__OP14mo ago
I thought about this, but I'm not sure if/how that works with keeping the field private, the constructor internal, and the whole type public
Thinker
Thinker14mo ago
I guess
__dil__
__dil__OP14mo ago
Does that mean it's not possible? (sorry I'm not a native english speaker so it's not super clear what "I guess" means in this context 😅 )
Thinker
Thinker14mo ago
No I meant your point is valid as to why you shouldn't use a record here, my bad
__dil__
__dil__OP14mo ago
Ah ok I see. Thanks for the help 😄 I'll implement what you suggested 🙂
reflectronic
reflectronic14mo ago
you can still use a record in fact, this would do what you want
public readonly record struct InternerId
{
private readonly int _id;

internal InternerId(int id)
{
_id = id;
}
}
public readonly record struct InternerId
{
private readonly int _id;

internal InternerId(int id)
{
_id = id;
}
}
__dil__
__dil__OP14mo ago
oh interesting! So, for the purpose of equality and such, records consider all their fields/properties?
reflectronic
reflectronic14mo ago
the auto-generated equality is based on all the fields, right
__dil__
__dil__OP14mo ago
ok yeah, that's good to know definitely reduces boilerplate here thanks!
reflectronic
reflectronic14mo ago
you can reduce the boilerplate even further with a primary constructor
public readonly record struct InternerId(int id)
{
private readonly int id = id;
}
public readonly record struct InternerId(int id)
{
private readonly int id = id;
}
when you assign a primary constructor parameter directly to a field or property with the same name, it prevents the public property from being generated it does sort of break the _ convention. i think it is a dumb convention and you will run into pitfalls like this if you try to stick with it. but that is just my strong opinion
__dil__
__dil__OP14mo ago
I think this is slightly different though since the constructor would also be public (instead of internal?
reflectronic
reflectronic14mo ago
uh. oh
__dil__
__dil__OP14mo ago
but still really good to know nonetheless
reflectronic
reflectronic14mo ago
well then ignore all of that
__dil__
__dil__OP14mo ago
I mean, it's still good to know in general but yeah
Thinker
Thinker14mo ago
is it?
reflectronic
reflectronic14mo ago
yes this includes auto-generated backing fields, of course
MODiX
MODiX14mo ago
reflectronic
sharplab.io (click here)
public readonly record struct InternerId(int id) {
private readonly int id = id;
}
public readonly record struct InternerId(int id) {
private readonly int id = id;
}
Try the /sharplab command! | React with ❌ to remove this embed.
reflectronic
reflectronic14mo ago
you can look at the implementation of Equals here to see what is going on
Thinker
Thinker14mo ago
ah thought it only looked at props
__dil__
__dil__OP14mo ago
So, just to be sure, for the purpose of the original question, this would be the way to go, right?
public readonly record struct InternerId
{
private readonly int _id;

internal InternerId(int id)
{
_id = id;
}
}
public readonly record struct InternerId
{
private readonly int _id;

internal InternerId(int id)
{
_id = id;
}
}
reflectronic
reflectronic14mo ago
right
__dil__
__dil__OP14mo ago
Alright, thank you both 🙂 by the way, is there a way to mark the thread as "solved"? 🤔
reflectronic
reflectronic14mo ago
$close
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server