So I am utilizing CollectionBase as an inherited class for custom collections. I am utilizing CollectionBase through an abstract class so that I don’t repeated knowledge (following the DRY principle). The abstract class is defined as a generic class also. Here is how I am implementing my class:
public abstract class GenericCollectionBase<T,C> : CollectionBase { //Indexders, virtual methods for Add, Contains, IndexOf, etc }
I utilize this so I don’t have to implement these base methods in 10+ classes.
My question is am I taking this too far when I override the Equals method like this:
public override bool Equals(object obj) { if (obj is C) { GenericCollectionBase<T, C> collB = obj as GenericCollectionBase<T, C>; if (this.Count == collB.Count) { for (int i = 0; i < this.Count; ++i) { if (!this[i].Equals(collB[i])) return false; } return true; } } return false; }
Am I trying to accomplish too much with my abstract, or doing this the correct way?
EDIT: This is written for .Net 2.0 and do not have access to 3.5 to utilize things like LINQ
I don’t believe you are trying to accomplish too much. If an abstract class was meant to not have any implementation at all, or other methods which define functionality, then they would be interfaces.
The only thing I would change is to use
EqualityComparer<T>instead of equals for the comparison ofthis[i]andcollB[i].