C
C#•2mo ago
FunHyper

Is List.Contains(Object) checks by address or by value?

Hi, if I have the following List/Dictionary (I know it doenst matter in this question): I want to check if a drink is inside the dictionary like this - Drinks.ContainsKey(drink) I'm not sure if it will check by value like if all the values inside the drink are equal or it will check if this is the identical drink (by adress). I want to point out that I overrided Equals inside of Item. Thank you🙏
No description
No description
4 Replies
blueberriesiftheywerecats
for ContainsKey its Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.
blueberriesiftheywerecats
GitHub
referencesource/mscorlib/system/collections/generic/dictionary.cs a...
Source from the Microsoft .NET Reference Source that represent a subset of the .NET Framework - microsoft/referencesource
FunHyper
FunHyperOP•2mo ago
Thanks!
br4kejet
br4kejet•2mo ago
The list is just going through all items and calling the Equals method on I think the parameter and it passes in the next items in the list (might be flipped, can't remember) until Equals returns true. If you don't override it then it uses the base object's Equals which is by address Dictionary ContainsKey is the same but uses the hash code to do the internal calculations first before calling Equals
Want results from more Discord servers?
Add your server