✅ Check if an item is in a list
For this if statement, how do i make it so if the parameter customer is not in the list, then it adds it to the list
20 Replies
.Contains()
for exampleAH
Or
.Any()
with a predicategotcha thank you!
Or use a hashset, it deduplicates automatically
got it thanks!
Not sure how well it works with reference types, though. Might need either some overrides on the type or a custom comparer for the hashset
if (!_customers.Contains(customer))
{
_customers.Add(customer); }
}
i mean this works right?
Well, reference types use reference comparison
So it works if a reference to that particular
customer
exists on the _customers
listah ok
i havnt learnt hashsets so im good
Angius
REPL Result: Success
Result: bool
Compile: 426.010ms | Execution: 42.391ms | React with ❌ to remove this embed.
As you can see
Different references
oh i see
oh right
well they are named different
Values inside of the class don't matter whatsoever, only the reference
right
When it comes to
.Contains()
at leastok gotcha
.Any()
with a predicate might be saferAngius
REPL Result: Success
Result: bool
Compile: 425.404ms | Execution: 102.865ms | React with ❌ to remove this embed.
its just a little program, it works so its fine haha
oooo i see
thanks!