[C # advanced series] 15 arrays,

Source: Internet
Author: User

[C # advanced series] 15 arrays,

First, I will mention that arrays are rarely used in my project, and List is used in more cases <>.

The array size is fixed. If it is only used to store data, it is specially used for reading, and the change is convenient. However, in more cases, we need to add, delete, and modify data. In this case, using List <> is better.

All Array types are implicitly derived from the System. Array abstract class, and the latter is derived from System. Object.

The birth of an array

String[] arr=String[10];

When the preceding statement is executed, CLR automatically creates a String [] type for the AppDomain. This type is implicitly derived from System. Array, so it can use the methods defined in System. Array.

All arrays implement IEnumerable, ICollection, and IList implicitly, because System. Array also implements these three interfaces.

When a one-dimensional zero-base array type is created, CLR automatically sets the array type to the current IEnumerable, the ICollection and IList interfaces are generic based on the basic type and its base class (here the basic type refers to the String and its base class Object,

IEnumerable <String>, ICollection <String>, and IList <String>, IEnumerable <Object>, ICollection <Object>, and IList <Object>. (and System. array is not implemented because it involves multi-dimensional arrays and Non-zero-base arrays ). The 0-base array is an array starting with 0 for index groups.

However, if the basic type of the array is the value type, that is, int [], it will not implement generic interfaces, but will only implement those three non-generic interfaces.

The array is always a reference type, so it will be allocated on the stack instead of allocated on the stack like c.

Creating an array of reference types actually creates an array filled with references, but the actual reference type is not created. These references are null by default.

Three Arrays

We usually use the following three Arrays:

Int [] One-dimensional array = new int [5]; int [,] multi-dimensional array = new int [3, 4]; int [] [] staggered array = new int [2] []; staggered array [0] = new int [10]; staggered array [1] = new int [100];

Array type conversion

Array can also be converted to the basic type. For example, the String [] type can be converted to the Object [] type.

However, the transformation requires that the array dimensions are the same, and CLR cannot convert an array of value types to any type. (However, Array. Copy can be used to convert the Array of value type)

If you only need to copy some elements of an array to another array of the same type, consider System. the BlockCopy method of Buffer. When you look at these strange names, you will know that it is the underlying operation, which is better than Array. fast Copy. However, it cannot provide transformation capabilities like Array. Copy, such as converting Object [] to Int [].

Array transfer and return

The array is passed as a parameter. In fact, only the array reference is passed.

If the return array reference method is defined and the array does not contain elements, null can be returned, but a new int [0] is recommended.

Create a non-zero-base Array

To be honest, I didn't plan to write it. I really don't know where to use it. If it is used to increase the difficulty of code reading, it would be a good choice to forcibly write junk code in order to install B.

The Array. CreateInstance method can be used, but it feels good to dynamically create an Array. Actually, you just need to know it. Generally, you cannot use it. List <> is much simpler and more convenient.

Array with lower limit and unknown lower limit

CLR supports two types of arrays. One is a one-dimensional zero-base array, and the other is an unknown one-dimensional array and a multi-dimensional array.

Generally, the array type is used. For example, the type of the 0-base array is System. String [], and the type of the non-0-base array is System. String [*].

The element accessing a one-dimensional 0-base array is slightly faster than that of a non-zero-base or multi-dimensional array. Some special IL commands are used to process a one-dimensional 0-base array, which causes the JIT compiler to generate optimization code.

In fact, staggered arrays are actually multiple one-dimensional arrays, which are faster than multi-dimensional arrays. Therefore, you can use staggered arrays to replace multi-dimensional arrays to improve performance.

 

PS:

CLR via C # also introduces how to operate arrays in unsafe ways:

The array can be directly embedded into the structure instead of as a reference object,

You can also use the stackalloc statement to allocate arrays on the thread stack, instead of allocating arrays on the stack as before.

However, this method is generally good, mainly used for non-hosted code interoperability.

Because it is in the unsafe mode, I don't even have to think about it, it's not a problem, it's not completely.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.