Arch of C #

xiaoxiao2021-03-06  56

Array

Array elements and element type concept C # amplitude set index value is the array starting from 0 is a collection of reference type arrays inherited by abstract type system.Array is the collection of elements of the same data type. Each element has a sequence in memory. According to the new features of this order C # array: a. The declaration method is different, [] To put it behind, if int [] table, not int Table [] b. Array size declaration] No size declaration Placed in [], it is dynamic, you can use the following method: int [] NumBers; NumBers = new int [10]; NumBers = new int =

1. Declaration of a one-dimensional array of declaration int [] intArray; no memory is allocated for array elements, so [] cannot indicate a number of array elements, ie array lengths, and any element that cannot be accessed. You must use New as It assigns memory space: INTARRAY = new int [4]; of course, you can also be together: int [] INTARRAY = New int [4]; 2. Declaration of multi-dimensional array is an array of arrays String [,] arrayname; int [, ] MyArray = new int [4, 2]; 3. Declaration of the sawdum array Byte [] [] score;

Byte [] [] score = new byte [5] []; for (int i = 0; i

It is also possible to mix the sawtooth data and rectangular arrays together Int [] [,,] [,] NumBers;

Array is an object, you must instantiate it later.

II. Array Initialization 1. Create an array space IntArray = new int [10]; can also be combined with the declaration number, complete: int [] INTARRAY = new int [10]; can also create an array space At the same time, give the initial value: int [] MyintArray = {1, 2, 4, 5};

2. Initialization of an array a. Initialization int [] number number = new int [5] {1, 2, 3, 4, 5}; INT [] NumBers = new int [] {1, 2, 3, 4, 5}; if there is initialization data, then NUMBERS = {1, 2, 3, 4, 5} can also be ignored;

b. 2D data Similar to one-dimensional int [,] NumBers = new int [3, 2] {{1, 2}, {3, 4}, {5, 6}}}; int [,] NumBers = new int = , {1, 2}, {3, 4}, {5, 6}}; int [,] numbers = {{1, 2}, {3, 4}, {5, 6}}

c. Initialization INT [] [] NumBers = new int [2] [] {new int = {2, 3, 4}, new int [] {5, 6, 7, 8, 9}} The first dimension can also be ignored: int [] [] NumBers = new int [] {new int} {2, 3, 4}, new int}}} {5, 6, 7, 8, 9}} ;

3. Access array member a. Use ordinary methods to access array members: NumBers [1,1] = 4; (as a zigzag array) b. Use the foreach -in statement loop access array foreach (int i in numbers) {}

4. Array is an object, so you can use the properties and methods in the System.Array class: NumBers.Length;

5. Transfer array parameters can pass the initial array to the method: PrintArray (MyArray); can also initialize and pass a new array in one step: PrintArray (new int]] 1, 2, 3});

6. Use REF and OUT to pass an array public static void mymethod (out int "arr) {arr = new int [10]; // explicitly assign a value} Ref, can perform initialization or have been initialized

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

New Post(0)