I am creating a generic class to hold widgets and I am having trouble implementing the contains method:
public class WidgetBox<A,B,C>
{
public bool ContainsB(B b)
{
// Iterating thru a collection of B's
if( b == iteratorB ) // Compiler error.
...
}
}
Error: Operator '==' cannot be applied to operands of type 'V' and 'V'
If I can not compare types, how am I to implement contains? How do dictionaries, lists, and all of the other generic containers do it??
You have a few options here
The first is to use Object.Equals
:
if(b.Equals(iteratorB)) {
// do stuff
}
Be careful using this option; if B
does not override Object.Equals
then the default comparsion is reference equality when B
is a reference type and value equality when B
is a value type. This might not be the behavior that you are seeking and is why without additional information I would consider one of the next two options.
The second is to add a constraint that B
is IComparable
:
public class WidgetBox<A, B, C> where B : IComparable
so that
if(b.CompareTo(iteratorB) == 0) {
// do stuff
}
A third is to require an IEqualityComparer<B>
be passed to the constructor of WidgetBox
public class WidgetBox<A, B, C> {
IEqualityComparer<B> _comparer;
public WidgetBox(IEqualityComparer<B> comparer) {
_comparer = comparer;
}
// details elided
}
Then:
if(_comparer.Equals(b, iteratorB)) {
// do stuff
}
With this last option you can provide an overload that defaults to EqualityComparer<T>.Default
:
public WidgetBox() : this(EqualityComparer<T>.Default) { }
Not all objects implement == but all will have Equals (although it may be inherited from Object.Equals).
public class WidgetBox<A,B,C>
{
public bool ContainsB(B b)
{
// Iterating thru a collection of B's
if( b.Equals(iteratorB) )
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With