Labels

Monday, March 9, 2009

Collection Myth - CollectionBase, ICollection, IEnumerable

Hi,

 

Custom collection classes ca be implemented by inheriting from one of the many .NET Framework collection classes and adding code to implement your own custom functionality.

The interfaces that supports custom collection creation are –

 

-          CollectionBase

-          ICollection

-          IEnumerable

 

 

CollectionBase

 

-          This is one of the simplest way to implement your custom collections.

-          The CollectionBase class already has implementations for the Clear method and the Count property, and it maintains a Protected property called List, which it uses for the internal storage and organization. Other methods, such as Add and Remove, as well as the Item property, require implementation.

 

ICollection

 

-          The ICollection interface inherits from the IEnumerable interface. Thus the ICollection also inherits the GetEnumerator method from the this interface

-          This leads to implementing the members of both the Interfaces viz. ICollection & IEnumerable while implementing ICollection.

-          The ICollection interface defines a CopyTo method and three read-only properties: IsSynchronized, SyncRoot, and Count.

-          The IEnumerable defines the ‘GetEnumerator’ method.

-          A custom collection class should implement the ICollection interface.

 

 

IEnumerable

 

-          The ideal way to implement our own custom colletions is using the ‘ICollection’ interface as described above.

 

Here are the details for ICollection Interface –

 

-          The ICollection interface inherits from the IEnumerable interface. Thus the ICollection also inherits the GetEnumerator method from the this interface

-          This leads to implementing the members of both the Interfaces viz. ICollection & IEnumerable while implementing ICollection.

-          The ICollection interface defines a CopyTo method and three read-only properties: IsSynchronized, SyncRoot, and Count.

-          The IEnumerable defines the ‘GetEnumerator’ method.

 

-          However in your collection,  if you do not need the support for the ICollection members then instead of inheriting thru ICollection, the other alternative coud be just to implement IEnumerable interface for implementing the custom collection.

-          The IEnumerable defines the ‘GetEnumerator’ method only.

 

 

Few Lines from MSDN

Reference: In local MSDN, search for ‘ICollection interface, defining collections’

All collections that directly or indirectly implement the ICollection interface or the ICollection<(Of <(T>)>) generic interface share several features in addition to methods that add, remove, or search elements:

 

An enumerator

-          An enumerator is an object that iterates through its associated collection.

-          It can be thought of as a movable pointer to any element in the collection.

-          An enumerator can be associated with only one collection, but a collection can have multiple enumerators.

-          The C# foreach statement uses the enumerator and hides the complexity of manipulating the enumerator.

 

Synchronization members.

-          Synchronization provides thread safety when accessing elements of the collection.

-          The collections are not thread safe by default.

-          Only a few classes in the System.Collections namespaces provide a Synchronize method that creates a thread-safe wrapper over the collection.

-          However, all classes in all System.Collections namespaces provide a SyncRoot property that can be used by derived classes to create their own thread-safe wrapper.

-          An IsSynchronized property is also provided to determine whether the collection is thread safe.

-          Synchronization is not available in the ICollection<(Of <(T>)>) generic interface.

 

The CopyTo method.

-          All collections can be copied to an array using the CopyTo method; however, the order of the elements in the new array is based on the sequence in which the enumerator returns them.

-          The resulting array is always one-dimensional with a lower bound of zero.

 

Note that the ICollection<(Of <(T>)>) generic interface has additional members, which the nongeneric interface does not include.

 

 

Regards,

Arun Manglick

No comments:

Post a Comment