Home  >  Article  >  Backend Development  >  Common methods of C# dynamic arrays

Common methods of C# dynamic arrays

高洛峰
高洛峰Original
2016-12-16 14:52:562137browse

C# arrays have a lot to learn. Here we mainly introduce C# dynamic arrays, including introducing the common properties of C# dynamic arrays.

C# dynamic array object is a relatively complex array. We can think of it as an array with expanded functions, but ArrayList is not equivalent to an array. Compared with an array, what are its following functions and differences? Let's take a look at the specific content:

1. The capacity of the array is fixed, but the capacity of ArrayList can be automatically expanded as needed. When we modify the capacity of ArrayList, we can automatically reallocate memory and copy elements. For example, insert n elements into index position No. 1. After the insertion, the index of the elements is arranged n positions backward. It is a dynamic version. Array type.

2.ArrayList provides methods to add, insert or remove elements in a certain range. But in an array, you can only get or set the value of one element at a time, such as using index assignment.

3.ArrayList has only one dimension, while arrays can be multi-dimensional.

How to declare a C# dynamic array?

ArrayList AL=new ArrayList(Capacity); //The initial capacity capacity does not need to be written.

The reason is that even if the capacity is not determined during initial recognition, when the capacity is not enough, it will automatically expand by multiples.

Commonly used properties of C# dynamic arrays
◆Capacity: Get or set the number of elements that ArrayList can contain.
◆Count: Get the actual number of elements contained in the ArrayList.
◆IsReadOnly: Gets a value indicating whether the ArrayList is read-only.
◆Item: Get or set the element at the specified index.

Common methods for dynamic arrays in C#
◆Add elements-ArrayList.Add(value); Use the Add method to increase the value of collection elements. We can also add elements by index ArrayList[Index]=value;
◆Insert elements-ArrayList.Insert( Index, value); Insert the value of the element into the Index position.
◆Delete elements-ArrayList.Clear(); Delete all elements in the collection
◆ArrayList.Remove(value); Delete elements according to the collection element value
◆ArrayList.RemoveAt(Index); Delete elements according to the element index of the collection
◆ Reduce capacity - ArrayList.TrimToSize(); Reduce the capacity of the collection to the actual number of elements

After performing the deletion operation, you must develop a good habit of reducing capacity to save memory space and improve performance.

Find elements - In addition to searching by the index of the array, you can also use ArrayList.Contains(value); search the collection according to the element value, and return True if it is contained, and False if it is not contained.

The basic situation of C# dynamic arrays is introduced to you here. I hope it will be helpful for you to learn and understand C# dynamic arrays.


For more related articles on common methods of C# dynamic arrays, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn