1. array Overview
An array is a reference type, which means that the memory of the array is allocated to the managed stack, and we maintain its pointer on the stack rather than the real array.
Next, we will analyze the elements of the array. The elements are reference type and value type.
When the element in the array is of the value type, it is different from the Code such as int I. The array automatically initializes the element value to its default value based on the array size. For example:
static void Main(string[] args){ int[] intArray = new int[3]; foreach(int i in intArray) { Console.WriteLine(i); } DateTime[] dtArray = new DateTime[3]; foreach (DateTime i in dtArray) { Console.WriteLine(i); }}
The result is as follows:
When the element in the array is of reference type, the element in the array is actually a pointer to the actual memory space of the object, occupying 4 bytes of space.
2. Zero-base Array
Since learning C language, I believe the teacher will tell us that the first index of the array is 0, not 1. However, in C #, we can construct a non-zero base array. In this section, Let's explain this.
In the general sense, we Initialize an array, which is a zero-base array by default. This also makes the number a special type after the string is initialized. As we know, when initializing a string, the corresponding IL command is newstr. Similarly, the IL command for initializing a zero-base array is newarr.
When we want to construct a non-zero base array, we can do the following:
static void Main(string[] args){ Array intArr = Array.CreateInstance(typeof(Int32), new int[] { 5 }, new int[] { 1 }); Console.WriteLine(intArr.GetValue(1).ToString()); Console.WriteLine(intArr.GetValue(0).ToString());}
The test results are as follows:
So it turns out that we initialized a non-zero base array. In addition, we should also remember the following two methods:
static void Main(string[] args){ Array intArr = Array.CreateInstance(typeof(Int32), new int[] { 5 }, new int[] { 1 }); Console.WriteLine(intArr.GetLowerBound(0)); Console.WriteLine(intArr.GetUpperBound(0));}
The test result is as follows:
3. efficiency issues
I believe there will be a lot of conspiracy commentators saying that C # is a type of secure language, that is, it means that every time I access an array element during a loop, it is necessary to check whether this index will cause the array to cross-border, as a result, performance loss is caused. So here we will give a thorough explanation of this problem.
Here we will divide the array into zero-base array, non-zero-base array, multi-dimensional array, and staggered array to discuss this issue separately.
The zero-base array is. NET, and the special IL command newarr is provided during initialization, which fully demonstrates that. NET. NET Framework will also provide a lot of optimization benefits for it. The following code is used to access arrays cyclically:
static void Main(string[] args){ int[] intArr = new int[5]; for (int i = 0; i < 4; i++) { //Some Method }}
The JIT compiler checks the relationship between 4 and intArr. GetUpperBound only once before the loop starts, and then does not intervene in the relationship. That is to say, the JIT compiler only checks security once, so the performance loss is very small.
For a non-zero base array, we can compare the two code segments:
static void Main(string[] args){ Array intArr = Array.CreateInstance(typeof(Int32), new int[] { 5 }, new int[] { 1 }); Console.WriteLine(intArr.GetValue(1).ToString()); Console.WriteLine(intArr.Length); // int[] intArr1 = new int[5]; Console.WriteLine(intArr1[1]); Console.WriteLine(intArr1.Length);}
In fact, they create almost the same Array and call almost the same method. However, we can see that IL has a striking difference between the two, the first is the non-zero base array IL:
Next is the zero-base array:
We can find that for most of the operations in the non-zero base array,. NET Framework provides the corresponding IL commands, and we can also understand that. NET Framework provides special optimization for them.
When