Article Directory
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:
-
Add adds the object to the end of the List.
-
AddRange adds the elements of the specified collection to the end of the List.
-
AsReadOnly returns the read-only IList wrapper of the current collection.
-
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.
-
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.
-
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.
-
Clear removes all elements from the List.
-
Contains determines whether an element is in the List. (Note to make a distinction with Exists)
-
Exists determines whether the List contains elements that match the conditions defined by the specified predicate.
-
ConvertAll converts the elements in the current List to another type, and returns a list containing the converted elements.
-
CopyTo(T[]) copies the entire List to a compatible one-dimensional array, starting from the beginning of the target array.
-
Find searches for elements that match the conditions defined by the specified predicate, and returns the first matching element in the entire List.
-
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.
-
ForEach performs the specified operation on each element of the List. GetEnumerator returns an enumerator that iterates through the List.
-
IndexOf(T) searches for the specified object and returns the zero-based index of the first matching item in the entire List.
-
Insert inserts the element into the specified index of the List.
-
InsertRange inserts an element in the collection into the specified index of the List.
-
LastIndexOf(T) searches for the specified object and returns the zero-based index of the last matching item in the entire List.
-
Remove Removes the first occurrence of a specific object from the List.
-
Reverse() reverses the order of the elements in the entire List.
-
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