C # Set -- ICollection interface and IList Interface

Source: Internet
Author: User

Although the list interface provides a protocol for traversing a set in the forward mode, they do not provide a mechanism to determine the size of the Set, access the members of the set through the index, search for the set, or modify the set. To implement these functions,. NET Framework defines the ICollection, IList, and IDictionary interfaces. Each interface has a Generic interface and a non-Generic interface. Note that most non-Generic interfaces are used to support legacy code.

Shows the inheritance and link of these interfaces:

The gap between interfaces of Generic and non-Generic exceeds your expectation, especially between ICollection and ICollection <T>. This is due to historical reasons, because the Generic type is C #2.0, so Generic learns from the experience and lessons of non-Generic interfaces, thus, different but better interfaces are designed. Because of this reason, ICollection <T> is not derived from ICollection. Similarly, IList <T> is not derived from IList, IDictionary <TKey, TValue> is not derived from IDinctionary. Therefore, the collection class can implement both the Generic interface and the non-Generic interface. (In fact, Collection classes generally implement two types of interfaces, such as class Collection <T>: IList <T>, IList, such as List <T>: IList <T>, System. collections. IList ).

 

ICollection <T> and ICollection

ICollection <T> is a standard interface for collecting statistics on objects. This interface can determine the set size (Count), whether the set Contains a certain element (Contains), copy the set to another array (ToArray), and whether the set is read-only (IsReadOnly ). If a set is editable, you can call the Add, Remove, and Clear methods to operate the elements in the set. Because this interface inherits IEnumerable <T>, you can use the foreach statement to traverse the set. The interface is defined as follows:

  ICollection<T> : IEnumerable<T>     Count {  IsReadOnly {       CopyTo(T[] array,     

ICollection instead of Generic is defined as follows:

  CopyTo(Array array,  Count {  IsSynchronized { 

Compared with ICollection <T>, ICollection provides the function of calculating the number of elements in a set, but does not provide the function of changing the set. In addition, ICollection also provides the synchronization function. The ICollection <T> Removes the synchronization function because the collection of Generic is thread-safe.

These two interfaces are simple and easy to implement. If you want to implement a read-only ICollection <T>, you can throw an exception in the Add, Remove, and Clear methods.

These interfaces are usually implemented together with any of the IList or IDictionary interfaces.

 

IList <T> and IList

If you want to obtain the collection element by location, IList <T> is the standard interface of this set. In addition, because IList <T> inherits ICollection <T> and IEnumerable <T>, this interface also provides the ability to read or write elements by location, or insert or delete an element at the specified position. IList <T> is defined as follows:

  IList<T> : ICollection<T>[ index] { ;  Insert( RemoveAt(

The IndexOf method performs linear search on the set. If no specified element is found,-1 is returned.

Implementation of the IndexOf method of the List class: (List. indexOf (T item) calls Array. indexOf (_ items, item, index, _ size-index), and then calls EqualityComparer <T>. default. indexOf (array, value, startIndex, count ))

   IndexOf(T[] array, T value,  startIndex,  endIndex = startIndex + ( i = startIndex; i < endIndex; i++ (Equals(array[i], value))  -

Rather than Generic IList, it contains more members because it inherits ICollection

 [ index] {  ; IsReadOnly  {  IsFixedSize  {  Insert( RemoveAt(

The Add method of the IList interface returns an integer, which is the position of the element added to the set. The Return Value of the Add method of the IList <T> interface is null.

In C #, List <T> is a typical class that implements both IList <T> and IList.

  List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>

The C # array also implements the IList interface of generic and non-generic. C # part of the code of the Array class (code that implements the IList Interface)

    {    {  [ {   NotSupportedException(Environment.GetResourceString( Array.IndexOf(, value) >= .GetLowerBound(, .GetLowerBound(),  Array.IndexOf( IList.Insert(  NotSupportedException(Environment.GetResourceString(  NotSupportedException(Environment.GetResourceString( IList.RemoveAt(  NotSupportedException(Environment.GetResourceString(

The Generic IList <T> is implemented in sealed class SZArrayHelper. SZ (Single dimensional, Zero-based ). Note that if you call the add or remove Methods technically or technically, A NotSupportedException exception is returned.

IReadonlyList <T>

To interoperate with the system set during Windows running, Framework4.5 introduces a new set interface IReadOnlyList <T>. This interface is very useful. It can also be seen as a scaled-down version of IList and is only available for read-only operations. It is defined as follows:

  IReadOnlyCollection< T> : IEnumerable<T> Count {   IReadOnlyList< T> : IReadOnlyCollection<T>[ index] { 

Because the type parameter is only used for the output position, it is marked as covariant ). For example, a cats list can be viewed as a read-only list of animals. In IList <T>, T is not marked as a covariant because T is applied to the input and output locations.

You may think that IList <T> is derived from IReadonlyList <T>, and Microsoft does not, this is because the IList <T> member needs to be moved to the IReadonlyList <T>, which brings heavy changes to CLR4.5 (programmers need to re-edit the program to avoid running errors ). In fact, Microsoft manually added the implementation of the IReadonlyList interface to the implementation class of IList <T>.

In Windows, IVectorView <T> corresponds to the IReadonlyList <T> of. NET Framework.

 

 

Reference

Linear Search: http://en.wikipedia.org/wiki/Linear_search; http://blog.teamleadnet.com/2012/02/quicksort-binary-search-and-linear.html

Array Implementation IList: http://stackoverflow.com/questions/11163297/how-do-arrays-in-c-sharp-partially-implement-ilistt/11164210#11164210

The mysteries of array: http://stackoverflow.com/questions/19914523/mystery-behind-system-array

Covariant: http://stackoverflow.com/questions/2719954/understanding-covariant-and-contravariant-interfaces-in-c-sharp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.