C
C#2y ago
palapapa

❔ Why doesn't every method in Collection<T> has an extension point?

For example, Insert calls InsertItem, and InsertItem by default calls items.Insert(index, item);(source), where items is the IList<T> instance passed into its constructor. InsertItem is also virtual. This means that to customize Collection<T>.Insert, you can either override InsertItem, or pass an IList<T> instance into the constructor of Collection<T> that has the custom behavior you want. But for methods like Contains, there isn't an extension point like, for example, ContainsItem. That means the only way to customize Contains is to pass an IList<T> instance into the constructor of Collection<T> that has the custom behavior you want. This raises the question of why there are two ways to customize methods like Insert, Clear, Remove, and the setter of the indexer(SetItem), while there is only one for the other methods, like the getter of the indexer and Contains. Is there any specific reason for this?
3 Replies
Thinker
Thinker2y ago
Is there any specific reason you're using Collection<T> in the first place?
palapapa
palapapaOP2y ago
I wasn't. I just discovered it and learned that it's supposed to be extendible, but then I thought about this question I just asked and it felt really weird
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?