".NET Framework Design" Chapter 14 Array

xiaoxiao2021-03-06  88

Chapter 14 Array Content Summary:

This chapter discusses all aspects of the array, in-depth research in this common type.

First, an array introduction

Three types: one-dimensional array, multidimensional array, interleave array (jagged aray)

l Co-dimensional array:

INT32 [] Myintegers;

Myintegers = New INT32 [100];

l Multi-dimensional array:

INT32 [,] Myintegers;

Myintegers = new int32 [100,100];

l Interlaced array: Interlaced arrays are not supported by 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 description of the .NET Framework SDK

Third, array transformation

l The two arrays must have the same dimension

l There are implicit or explicit conversions between element types in the two arrays.

L In addition to using the array.copy () method, it is not allowed to convert value type arrays to other types of arrays (Array.copy methods will be enforced by mandatory type conversion or packing operations as needed)

The type converted by the array.copy () method is as follows:

l Convert value type to reference type, convert INT32 to Object

l Convert the reference type to the value type, convert the object to int32

l Widen (WIDEN) CLR base type, such as converting INT32 to Double

Below this example provides a negative textbook (do not follow the imitation, the consequences of the consequences!) The value type is replaced:

Using system;

/ / Customize a value type, this type can be converted with INT32

Struct myage

{

Private int

32 m

_nage;

Public myage (INT32 NAGE)

{

THIS.M_NAGE = NAGE;

}

// Convert operator

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 [] arrit = new int32 [10];

For (int i = 0; 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 ());

}

}

}

/*operation result

Unprocessed exception: System.ArrayTypeMatchException: You cannot assign the source array type to the target array

Types of.

At System.Array.copy (Array SourceArray, INT32 SourceIndex, Array DestinationArray, Int32 DestinationIndex, Int32 Length)

AT System.Array.copy (Array SourceArray, Array DestinationArray, INT32 Length)

AT Arraytest.main ()

* /

Note: 1. The above code cannot be run. Although the conversion operation is defined for the value type, the above conversion condition is still not met, and the conventional conversion operation is not recognized by Array.copy ().

2. Application of type conversion operators involved in the above code, please refer to the reading note according to the ninth chapter

Fourth, array pass and return

Precautions:

l For a deep copy of the array element, each element is required to implement an Icloneable interface. It should be noted that the deep copy operation should be used at the appropriate time (see "C # primer" p185)

l Returns an array reference method If an array element should return a array containing 0 elements instead of NULL

l Not returns an array reference within the method, but re-constructs a deep copy of the array returns.

5. Creating an array of lower limit non-0

Use the array.createInstance () method:

Public Static Array CreateInstance (Type ElementType, Int [] lengths, int [] lowerbounds

ELEMENTTYPE: The model of Array to create.

Lengths: One-dimensional array, it contains the size of each dimension of Array to create

LowerBounds: One-dimensional array, which contains the lower limit of each dimension of Array to create.

Sixth, fast array visits

Important:

l Use non-secure code

l Access arrays using pointers

l The element of the non-secure array operation is a numerical type, the Boolean, an enumeration type, or field is the structural type of the above type, or the field is the structural type of the above-described structural type ... (Recursive defined structural type)

As shown in the following routine:

Using system;

Public Class Arrsafe

{

Unsafe public static void main () // This point indicates the use of non-secure code

{

INT32 [] arr = new int32 [] {1, 2, 3, 4, 5};

FIXED (INT32 * Element = arr) ??? // Use a pointer access

{

For (INT32 I = 0; I

{

Console.writeline (Element [i]);

}

}

}

}

7. Re-adjust the length of the array

l Use the following method to get a new array length (see Detailed description in .NET Framework SDK)

Create an index from zero, with a one-dimensional Array specifying Type and length.

[C #] public static array createInstance (Type, Int);

Create an index from zero, with multi-dimensional array specified by Type and a long. The length of the dimension is specified in a 32-bit integer array.

[C #] public static array createinstance (Type, params int [];

Create an index from zero, with multi-dimensional array specified by Type and a long. The length of the dimension is specified in a 64-bit integer array.

[C #] public static array createinstance (Type, Params long []);

Create a 2D Array with an index from zero, with a 2D Array that specifies Type and a long. [C #] public static array createInstance (Type, int, int);

Create multi-dimensional array with a specified lower limit, specify Type and a warden.

[C #] public static array createInstance (Type, int [], int []);

Create a three-dimensional array that uses the index from zero, with a specified Type and a long.

[C #] public static array createInstance (Type, int, int, int);

l Then use the array.copy () method to copy the original array to the new array.

转载请注明原文地址:https://www.9cbs.com/read-122391.html

New Post(0)