This article mainly introduces the relevant information about the detailed explanation and differences between Array and ArrayList in C#. Friends in need can refer to
C# Detailed explanation and difference between Array and ArrayList
##1. Usage of Array
type[] typename=new type[size];
type[] typename=new type[]{ };
variableIt must be instantiated at the same time as the declaration (if initialized, at least the size of the array must be initialized)
Usually we int[], string[]...In fact, we declare aarray array了
Such as:string [] srt=new string[]{"a","b"}; int[] a=new int[2]; string [] srt=new string[3];
Data type cannot be missing; and must be unified, not such as int[] a =new Array[];
(2): The size of the array cannot be missing, otherwise C# will think it is an error because the array is a fixed-length memory;(3): Right It is a square bracket [], not ()Note: array array does not provide add, clear, addRange.. methods, but directly sets or gets the valueFor example:a[0] = 0; a[1] = 1;
2, Usage of C# ArrayList array:
var arrayList = new ArrayList(); arrayList.Add(1); arrayList.Add(2); arrayList.Add(50.0); //在.net 4.0 支持。具体为什么还没有研究 foreach (var array in arrayList) { Console.WriteLine(array); }
3, The conversion between ArrayList and Array
var arrayList = new List<int>(); arrayList.Add(1); arrayList.Add(2); arrayList.Add(50); //ArrayList 数组中的值拷贝到Array中去 int[] array1=new int[arrayList.Count]; arrayList.CopyTo(array1); //方法一 int[] array2 = arrayList.ToArray(); //方法二
4. [The difference between Array and ArrayList]
#1. Array type variables are declared at the same time It must be instantiated (at least the size of the array must be initialized), while ArrayList can just be declared first.int[] array = new array[3]; 或 int[] array = {1,2,3}; 或 ArrayList myList = new ArrayList();
objects, while ArrayList can store heterogeneous objects.
character data , except arrays declared as object[].
The initialization of the Array object must only be of the specified size, and the size of the array after creation is fixed,
5. [Similarities between Array and ArrayList]
#1 Both have indexes, that is, any item can be directly obtained and modified through index.#2 The objects they create are placed in the managed heap.
#3 can all enumerate themselves (because they all implement the IEnumerable interface).
6. [Some features of ArrayList]
var arrayList = new List<int>(2); Console.WriteLine(arrayList.Capacity); int size = 2; for (int i = 0; i < size; i++) { arrayList.Add(i); } Console.WriteLine("compressed capacity:"+arrayList.Capacity);
When size is When 3 or 4, "current capacity" is 4,
When size is 5~8, "current capacity" is 8,
When size is 9~16, "current capacity" is 16,
ArrayList myList = new ArrayList(5); for (int i = 0; i < 3; i++) { myList.Add(i); } Console.WriteLine("actual capacity:" + myList.Capacity); myList.TrimToSize(); Console.WriteLine("compressed capacity:" + myList.Capacity); Console.ReadLine();
actual capacity:5 compressed capacity:3
The above is the detailed content of Detailed explanation of the difference between Array and ArrayList in C#. For more information, please follow other related articles on the PHP Chinese website!