This chapter discusses all aspects of the array and makes an in-depth study of this common type.
An overview of the array
Three types: one-dimensional array, multidimensional array, staggered array (jagged Aray)
L One-dimensional arrays:
Int32[] myintegers;
Myintegers = new INT32[100];
L Multidimensional Arrays:
int32[,] myintegers;
Myintegers = new INT32[100,100];
L Jagged Arrays: jagged arrays not supported by the CLS
point[][] mypolygons = new point[3][];
Mypolygons[0] = new POINT[10];
MYPOLYGONS[1] = new POINT[20];
MYPOLYGONS[2] = new POINT[30];
Second, System.Array
Please refer to the relevant description in the. NET Framework SDK
Three, array transformation
L Two arrays must have the same number of dimensions
Implicit or explicit conversions between element types in the L two array
L not allowed to convert a value type array to another type of array except using the Array.copy () method (the Array.copy method enforces type conversions or boxing operations as needed)
The type conversion that the Array.copy () method can perform is as follows:
L Converts a value type to a reference type, converting Int32 to Object
L converts a reference type to a value type, converting object to Int32
L broaden (widen) CLR base type, such as converting Int32 to double
The following example provides a negative effect (do not follow the consequences!). ) The value type of the load Exchange:
Using System;
Customizes a value type that can be converted to Int32
struct Myage
{
Private Int32 m_nage;
Public Myage (Int32 nage)
{
This.m_nage = nage;
}
Conversion operators
public static implicit operator Myage (Int32 nage)
{
return new Myage (nage);
}
public static explicit operator Int32 (Myage age)
{
return age. ToInt32 ();
}
Public Int32 ToInt32 ()
{
return m_nage;
}
public override string ToString ()
{
Return "Age:" + m_nage;
}
}
public class Arraytest
{
public static void Main ()
{
int32[] Arrint = new INT32[10];
for (int i = 0; i < arrint.length i + +)
{
Arrint[i] = i;
}
myage[] Arrage = new Myage[arrint.length];
Array.copy (Arrint, Arrage, arrint.length);
foreach (Myage Age in Arrage)
{
Console.WriteLine (age. ToString ());
}
}
}
/* Run Results
Unhandled exception: System.ArrayTypeMismatchException: Cannot assign the source array type to the destination array
Type.
At System.Array.Copy (array sourcearray, Int32 sourceindex, array Destinationa
Rray, Int32 destinationindex, Int32 length)
At System.Array.Copy (array sourcearray, array destinationarray, Int32 length)
At Arraytest.main ()
*/
Note: 1, the above code is not operational. Although a conversion operation is defined for a value type, the above conversion condition is not satisfied, and the visible custom conversion operation is not recognized by array.copy ().
2, the above code involves the application of type conversion operators, please refer to the Nineth chapter of reading notes method
Iv. Array Delivery and return
Precautions:
To obtain a deep copy of the array elements, each element is required to implement the ICloneable interface. It should be noted that deep copy operations should be used in due course (see C # Primer for details) p185
L A method that returns an array reference should return an array of 0 elements instead of NULL if the array element is not included
L does not return an array reference inside the type of the method, but rather reconstructs a deep copy of the array return.
Create an array with a lower bound of not 0
Use the Array.CreateInstance () method:
public static Array CreateInstance (Type ElementType, int[] lengths, int[] lowerbounds);
ElementType: The Type of Array to create.
Lengths: A one-dimensional array that contains the size of each dimension of the array to be created
Lowerbounds: A one-dimensional array that contains the lower bound (starting index) of each dimension of the array to be created.
Six, fast array access
Points:
L Use unsafe code
L Use pointers to access arrays
L An element that can perform an unsafe array operation is a numeric primitive type, a Boolean, an enumerated type, or a struct type with a field of the above type, or a type of struct that is a field of the above struct type ... (type of structure that is recursively defined)
As shown in the following routine:
Using System;
public class Arrsafe
{
Unsafe public static void Main ()///here indicates the use of unsafe code
{
int32[] arr = new int32[]{1,2,3,4,5};
Fixed (Int32 *element = arr)//Use pointer access
{
for (Int32 i = 0; i < arr. Length; i + +)
{
Console.WriteLine (Element[i]);
}
}
}
}
Seven, resize the array length
Use the following method to get the new array length to be created (see detailed descriptions in the. NET Framework SDK)
Creates a one-dimensional Array that uses a zero-based index, with the specified Type and length.
[C #] public static Array CreateInstance (Type, int);
Creates a multidimensional Array with a zero-based index, a specified Type, and a dimension length. The length of the dimension is specified in an array of 32-bit integers.
[C #] public static Array CreateInstance (Type, params int[]);
Creates a multidimensional Array with a zero-based index, a specified Type, and a dimension length. The length of the dimension is specified in an array of 64-bit integers.
[C #] public static Array CreateInstance (Type, params long[]);
Creates a two-dimensional Array with a zero-based index, a specified Type, and a dimension length.
[C #] public static Array CreateInstance (Type, int, int);
Creates a multidimensional Array with the specified lower bound, specified Type, and dimension length.
[C #] public static Array CreateInstance (Type, int[], int[]);
Creates a three-dimensional Array with a zero-based index, a specified Type, and a dimension length.
[C #] public static Array CreateInstance (Type, int, int, int);
L then copy the original array to the new array using the Array.copy () method
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.