S
Samuel R. Neff
What's the advantage of inheriting from CollectionBase as opposed to
just implementing IList? It seems that it saves you from having to
implement a few properties (Clear, CopyTo, Count, GetEnumerator, and
RemoveAt) but the way it implements all the other things you need to
override seems overkill and counters the advantage of having an
extensible base class.
For example, the documentation example implementation of Remove:
Public Class Int16Collection
Inherits CollectionBase
...
Public Sub Remove(value As Int16)
List.Remove(value)
End Sub 'Remove
...
End Class
Resolves to this call sequence:
Int16Collection.Remove ->
CollectionBase.get_List ->
((IList)CollectionBase).Remove ->
CollectionBase.InnerList.Remove
Whereas if one implemented IList directly, without collection base,
the equivalent call sequence would just be
Int16Collection.Remove ->
Int16Collection.InnerList.Remove
And this comparison is typical for all other operations as well.
So what's the advantage of using CollectionBase? Does everyone use
it? What's downside to just implementing IList?
Of course the question becomes moot when 2005 is out with Generics,
but the question is pertinent now...
Thanks,
Sam
just implementing IList? It seems that it saves you from having to
implement a few properties (Clear, CopyTo, Count, GetEnumerator, and
RemoveAt) but the way it implements all the other things you need to
override seems overkill and counters the advantage of having an
extensible base class.
For example, the documentation example implementation of Remove:
Public Class Int16Collection
Inherits CollectionBase
...
Public Sub Remove(value As Int16)
List.Remove(value)
End Sub 'Remove
...
End Class
Resolves to this call sequence:
Int16Collection.Remove ->
CollectionBase.get_List ->
((IList)CollectionBase).Remove ->
CollectionBase.InnerList.Remove
Whereas if one implemented IList directly, without collection base,
the equivalent call sequence would just be
Int16Collection.Remove ->
Int16Collection.InnerList.Remove
And this comparison is typical for all other operations as well.
So what's the advantage of using CollectionBase? Does everyone use
it? What's downside to just implementing IList?
Of course the question becomes moot when 2005 is out with Generics,
but the question is pertinent now...
Thanks,
Sam