Interface descriptions and use of. NET collection types

Source: Internet
Author: User

This article describes the interface description and usage of the. NET collection type, which is simple enough to clarify the idea and the use specification of the collection type as a parameter and return value, and more discussion about the collection type can be read as follows: The collection of. Net.

NET class Library provides a wealth of collection data types, and the optical system.collections namespaces provide: lists, queues, bit arrays, hash tables, and dictionaries, and many other collection types. In terms of the collection types under this namespace, there are generally two categories: lists, and dictionaries. The former accesses the data in the collection sequentially, which provides access to key-value pairs.

This article mainly introduces the description of the interface of list type and the suggestion of using it, and does not involve the generics in net2.0, the interface level of the latter is similar to the introduction here.

Interface Definition:

IEnumerator
  object Current { get; }
  bool MoveNext();
  void Reset();IEnumerable
  IEnumerator GetEnumerator();
ICollection : IEnumerable
  int Count { get; }
  bool IsSynchronized { get; }
  object SyncRoot { get; }
  void CopyTo(Array array, int index);

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

Description

IEnumerator provides a way to traverse the data, which is used when you need to access the data in the list in turn.

IEnumerable provides support for the foreach operation and returns the IEnumerator interface object.

ICollection provides the number of entries attribute, with the method of copying to the array, while providing thread synchronization support.

IList provides add, find, delete, and empty operations, and supports indexed access, while IList has read-only and fixed-length attributes, which can cause exceptions if new additions are performed for read-only or fixed-length conditions.

Use:

1 as a parameter:

If you need to traverse the list, but do not need to modify the case, the priority to use ICollection, because collection than enumerable, enumerator is more appropriate on the name. At the same time, collection provides the Count property, which allows you to know the number of entries in a list without having to traverse it, which is more useful when making some logical judgments.

For example:

    bool Foo(ICollection list)
    {
      if (list == null)
        throw new ArgumentException("参数不能为空!", "list");

      if (list.Count == 0)
      {
        return false;
      }

      foreach(object item in list)
      {
        ... option
      }
      return true;
    }

If you need to modify the list data, find, and other operations, use IList.

2 as the return value:

If you want to return a read-only list, use ICollection instead of IList.

In most cases, IList is returned because the IList has a wider interface, and after the caller obtains a list of IList types, it is easier to pass the list back to a function that needs: ICollection or IList.

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.