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 –
-
- 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
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
- However, all classes in all
- An IsSynchronized property is also provided to determine whether the collection is thread safe.
- Synchronization is not available in the
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
Regards,
Arun Manglick
No comments:
Post a Comment