7.2 Standard Collection Interfaces

While IEnumerable and IEnumerator provide standard ways to access the contents of a collection, they don't provide any way to modify it, nor any way to easily perform other common tasks , such as determine the size, search the collection, etc. The .NET Framework also defines a set of three standardized interfaces ( ICollection , IList , and IDictionary ), which collections should implement to provide support for these types of operations.

7.2.1 ICollection Interface

The ICollection interface is the standard interface for countable collections of objects. It provides the ability to determine the size of a collection, to determine if it can be modified or synchronized, and to copy the collection into an array for sequential processing. Since ICollection extends IEnumerable , types that implement ICollection can also be traversed via IEnumerable and IEnumerator . The interface looks like this:

 public interface ICollection : IEnumerable {    void CopyTo(Array array, int index);    int Count {get;}    bool IsReadOnly {get;}    bool IsSynchronized {get;}    object SyncRoot {get;} } 

7.2.2 IList Interface

The IList interface is the standard interface for array-indexable collections. In addition to the functionality inherent in ICollection and IEnumerable , it also provides the ability to index directly into the collection by position (using the overloaded indexer), to add, remove, and change elements in the collection by position, and to search for elements in the collection. The interface looks like this:

 public interface IList : ICollection, IEnumerable {    object this[int index] {get; set}    int Add(object o);    void Clear( );    bool Contains(object value);    int IndexOf(object value);    void Insert(int index, object value);    void Remove(object value);    void RemoveAt(int index); } 

7.2.3 IDictionary Interface

The IDictionary interface is the standard interface for key/value-based collections such as hashtables, maps, and property bags. Similar to the IList interface, it provides the functionality inherent in ICollection and IEnumerable , as well as the ability to access elements in the collection based on key, remove them, search the collection for an element, and access lists of the keys and values already in the collection. The interface looks like this:

 public interface IDictionary : ICollection, IEnumerable {    object this[object key] {get; set};    ICollection Keys {get;}    ICollection Values {get;}    void Clear( );    bool Contains(object value);    IDictionaryEnumerator GetEnumerator( );    void Remove(object key); } 


C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net