The usage of collections in c#


1 Overview

1.1 Limitations of arrays

  • Arrays can only store data of the same type
  • The length of the array cannot be dynamically expanded

1.2 Classification of collections

Collections in C# are generally divided into two types, generic collections and non-generic collections. I generally use non-generic collections less. Mainly use generic collections


There are mainly several types of collections in C#:

ArrayList represents an array of objects, the size of these objects will dynamically increase as needed.
Hashtable represents a collection of key/value pairs organized according to the hash code of the key.
Queue represents a first-in first-out (FIFO) collection of objects.
Stack represents a last-in-first-out (LIFO) collection of objects.


2. Non-generic collection


3.List<T> Generic Collection

Features:

  • The strong type of the objects accessible through the index.

  • It is the generic equivalent of the ArrayList class.

  • An integer index can be used to access the elements in this collection; the index starts at zero.

  • Can receive a null reference (Nothing in VB).

  • Repeating elements are allowed.



List<T> collection method:

  1. Add adds the object to the end of the List.

  2. AddRange adds the elements of the specified collection to the end of the List.

  3. AsReadOnly returns the read-only IList wrapper of the current collection.

  4. BinarySearch(T) uses the default comparator to search for an element in the entire sorted List and returns the zero-based index of the element.

  5. BinarySearch(T, IComparer) searches for an element in the entire sorted List using the specified comparator, and returns the zero-based index of the element.

  6. BinarySearch(Int32, Int32, T, IComparer) uses the specified comparator to search for an element in an element range of the sorted List and returns the zero-based index of the element.

  7. Clear removes all elements from the List.

  8. Contains determines whether an element is in the List. (Note to make a distinction with Exists)

  9. Exists determines whether the List contains elements that match the conditions defined by the specified predicate.

  10. ConvertAll converts the elements in the current List to another type, and returns a list containing the converted elements.

  11. CopyTo(T[]) copies the entire List to a compatible one-dimensional array, starting from the beginning of the target array.

  12. Find searches for elements that match the conditions defined by the specified predicate, and returns the first matching element in the entire List.

  13. FindIndex(Predicate) searches for elements that match the conditions defined by the specified predicate and returns the zero-based index of the first matching element in the entire List.

  14. ForEach performs the specified operation on each element of the List. GetEnumerator returns an enumerator that iterates through the List.

  15. IndexOf(T) searches for the specified object and returns the zero-based index of the first matching item in the entire List.

  16. Insert inserts the element into the specified index of the List.

  17. InsertRange inserts an element in the collection into the specified index of the List.

  18. LastIndexOf(T) searches for the specified object and returns the zero-based index of the last matching item in the entire List.

  19. Remove Removes the first occurrence of a specific object from the List.

  20. Reverse() reverses the order of the elements in the entire List.

  21. Sort() uses the default comparator to sort the elements in the entire List.

The difference between Contains and Exist

See reference [5] for details.

references

[1] https://www.cnblogs.com/sujulin/p/7137938.html
[2] https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/collections
[3 ] https://blog.csdn.net/weixin_43914767/article/details/104569990?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.not_use_machine_learn_pai&depth_1-utm_source=distt.nist- -1.not_use_machine_learn_pai
[4] https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1?view=net-5.0
[5] https: //www.cnblogs .com / xu-yi / archive / 2019/06/23 / 11071148.html
[6] https://www.cnblogs.com/xu-yi/p/11026454.html

Guess you like

Origin blog.csdn.net/zhaozhao236/article/details/111148545