Arrays in C #

Source: Internet
Author: User
Array overview
An array is a data structure that contains several variables of the same type. An array is declared using a type:
The array has the following properties:

An array can be one-dimensional, multidimensional, or interleaved.

The default value of a numeric array element is set to zero, and the default value of the reference element is set to NULL.

A jagged array is an array of arrays, so its elements are reference types and are initialized to null.

The index of the array is zero-based: The index of an array with n elements is from 0 to n-1.

An array element can be any type, including an array type.

An array type is a reference type derived from an abstract base type array. Because this type implements IEnumerable and IEnumerable, you can use foreach iterations for all arrays in C #.

The C # array is indexed from zero, that is, the array index is zero-based. Arrays in C # work similar to how they work in most other popular languages. But there are some differences that should be brought to the attention.
When declaring an array, square brackets ([]) must follow the type, not after the identifier. In C #, placing square brackets after an identifier is not a valid syntax.

Int[] table; not int table[];

Another detail is that the size of the array is not part of its type, and you can declare an array and assign it an arbitrary array of int objects, regardless of the length of the array.

Int[] numbers; Declare numbers as an int array of any sizenumbers = new int[10];  Numbers is a 10-element arraynumbers = new INT[20];  Now it ' s a 20-element array

Declaring an array
C # supports arrays of one-dimensional arrays, multidimensional arrays (rectangular arrays) and arrays (jagged arrays). The following example shows how to declare arrays of different types:
One-dimensional arrays:

Int[] numbers;

Multidimensional Arrays:

string[,] names;

Array of arrays (interleaved):

Byte[][] scores;

declaring arrays (as shown above) does not actually create them.

In C #, an array is an object that must be instantiated. The following example shows how to create an array:
One-dimensional arrays:

int[] numbers = new INT[5];

Multidimensional Arrays:

string[,] names = new string[5,4];

Array of arrays (interleaved):

Byte[][] scores = new Byte[5][];for (int x = 0; x < scores. Length; X + +) {   scores[x] = new byte[4];}

You can also have larger arrays. For example, you can have a three-dimensional rectangular array:

Int[,,] buttons = new int[4,5,3];

You can even mix a rectangular array with a jagged array. For example, the following code declares a one-dimensional array of three-dimensional arrays of two-dimensional arrays of type int.

int[][,,][,] numbers;

Initializing an array
By enclosing the initial value in curly braces ({}), it provides a simple and straightforward way to initialize the array at the time of declaration. The following example shows various methods for initializing arrays of different types.
Note If the array is not initialized at the time of declaration, the array member is automatically initialized to the default initial value of the array type. Also, if an array is declared as a field of a type, it is set to the default value NULL when the type is instantiated.
One-dimensional arrays

int[] numbers = new Int[5] {1, 2, 3, 4, 5};string[] names = new String[3] {"Matt", "Joanne", "Robert"};

You can omit the size of the array, as follows:

int[] numbers = new int[] {1, 2, 3, 4, 5};string[] names = new string[] {"Matt", "Joanne", "Robert"};

If you provide an initializer, you can also omit the new operator, as follows:

int[] numbers = {1, 2, 3, 4, 5};string[] names = {"Matt", "Joanne", "Robert"};

Multidimensional arrays

int[,] numbers = new int[3, 2] {{1, 2}, {3, 4}, {5, 6}};string[,] siblings = new string[2, 2] {"Mike", "Amy"}, {"Mary", "Albert"};

You can omit the size of the array, as follows:

int[,] numbers = new int[,] {{1, 2}, {3, 4}, {5, 6}};string[,] siblings = new string[,] {{"Mike", "Amy"}, {"Mary", "Alber T "}};

If you provide an initializer, you can also omit the new operator, as follows:

int[,] numbers = {{1, 2}, {3, 4}, {5, 6}};string[,] siblings = {"Mike", "Amy"}, {"Mary", "Albert"};

Jagged Array (array of arrays)
You can initialize a jagged array as shown in the following example:

int[][] numbers = new int[2][] {new int[] {2,3,4}, new int[] {5,6,7,8,9}};

The size of the first array can be omitted, as follows:

int[][] numbers = new int[][] {new int[] {2,3,4}, new int[] {5,6,7,8,9}};

Or

int[][] numbers = {new int[] {2,3,4}, new int[] {5,6,7,8,9}};

Note that there is no initialization syntax for elements of a jagged array.

Accessing array members
Accessing an array member can be done directly, for example, the following code creates an array named numbers, and assigns 5 to the fifth element of the array:

int[] numbers = {Ten, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};numbers[4] = 5;

The following code declares a multidimensional array and assigns 5 to the member at [1, 1]:

int[,] numbers = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, ten}};numbers[1, 1] = 5;

The following declares a one-dimensional jagged array that contains two elements. The first element is an array of two integers, and the second element is an array of three integers:

int[][] numbers = new int[][] {new int[] {1, 2}, new int[] {3, 4, 5}};

The following statement assigns 58 to the first element of the first array and 667 to the second element of the second array:

Numbers[0][0] = 58;numbers[1][1] = 667;

An array is an object in C #, and an array is actually an object. System.Array is an abstract base type for all array types. You can use the properties that System.Array has and other class members. An example of this usage is to get the length of the array using the Length property. The following code assigns the length of the numbers array (5) to a variable named lengthofnumbers:

int[] numbers = {1, 2, 3, 4, 5};int lengthofnumbers = numbers. Length;

The System.Array class provides a number of useful other methods/properties, such as methods for sorting, searching, and copying arrays.

Using foreach with arrays
C # also provides a foreach statement. This statement provides a simple, straightforward way to iterate through the elements of an array.
For example, the following code creates an array named numbers and iterates through the array with a foreach statement:

int[] Numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0};foreach (int i in numbers) {   System.Console.WriteLine (i);}

Because of the multidimensional array, you can iterate through the elements using the same method, for example:

int[,] numbers = new int[3, 2] {{9, 3}, {5, 55}};foreach (int i in numbers) {   Console.Write ("{0}", i);}

The output of this example is:

9 99 3 33 5 55

However, because of the multidimensional array, using a nested for loop will give you more control over the array elements.


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.