web123456

The difference between array, list and Array List in C#

As we all know, it is inevitable to encounter some data storage and references in programming, so in C# there are arrays, List<> and Array List used to store these data and then reference them. Let's introduce these three:

1. Array: Arrays in C# can be used to store any data type. The following table of the array starts from 0, that is, the subscript corresponding to the first element is 0, and the next subscript is incremented in sequence. The array has one-dimensional and multi-dimensional;
One-dimensional array:
//The contents in the string array must be enclosed with ""
string[] str={"First Number","Second Number","Third number"};
//Integer array containing n elements
int[] first=new int[n];
//Array containing 6 custom known elements
int[] second=new int[6]{1,2,3,4,5,6};

Two-dimensional array:

//Two-dimensional integer array, the middle of the array is separated by commas
int[,] third=new int[,]{{1,5},{2,8}};
//The result is third[0,0]=1,third[0,1]=5,third[1,0]=2,third[1,1]=8

And so on, the representation of multidimensional arrays is expanded with commas, such as a two-dimensional array.[,]Similarly, three-dimensional is[,,]And so on;

<>:List<> is a generic collection, representing a strongly typed list of objects that can be accessed through an index, providing methods for searching, sorting and operating the list;
Example:
List<Border> border=new List<Border>();//The type of the list is Border
//Then you can perform a series of related operations on it, such as:Reference the value in it,By index border[i];The value of the i-th element can be obtained
 Delete border.Remove[i];The value of the i-th element can be deleted
 Add border.Add[i];Can add element i
List:Array List is a dynamic array, which is a complex version of Array. It provides dynamic addition and reduction of elements, implements ICollection and IList interfaces, and flexibly sets the size of the array, etc.;
Usage: First, you need to add the "`using ;`" class to the namespace; a simple example:
ArrayList arrayLists =new ArrayList();
//Add elements
arrayLists.AddRange();//Add ICollection element to the end of ArrayList
arrayLists.Add();//Add object to the end of ArrayList
//Delete the element
arrayLists.RemoveAt();//Remove the element at the index
arrayLists.Remove();//Remove the first match of a specific object
arrayLists.RemoveRange();//Remove elements within a certain range
arrayLists.Clear();//Remove all elements from ArrayList
//Get the element
arrayLists.Count.ToString();//Get the number of elements contained in the ArrayList
//Insert element
arrayLists.Insert();//Insert the element into the specified index of ArrayList
arrayLists.InsertRange();//Insert the element in the collection into the index specified by ArrayList

The above is a simple introduction to the differences between the three. If there are any shortcomings, I hope to give you valuable suggestions::