I have a custom class derived from List with an Add method that adds only if a certain condition is satisfied.
Do I also need to override* AddRange, or does AddRange simply call Add on each element of the given range?
*: Yes, new is hiding and not overriding in the context of C#.
If you want to create custom collection. Don’t derive it from
List<T>but fromCollection<T>or directly implementIList<T>orICollection<T>. Indeed, theAddmethod in theList<T>class is not virtual.Note:
List<T>.AddRangeusesArray.Copy.UPDATE
When inheriting Collection you just have to override 2 methods!
If your items to validate are not
stringreplacestringin the code above by the correct type.