Use of arrays in C #

Source: Internet
Author: User

1. Simple array:

statement:

int[] MyArray

Initialize:

Once an array has been declared, it must allocate memory for the array to hold all the elements of the arrays. It is particularly important to note that arrays are reference types. It is therefore necessary to use the new operator to specify the type and number of elements in the array to initialize the variables of the array.

MyArray = new Int[4]

Declaration Initialization Merge:

int [] MyArray = new Int[4];

use an initializer to assign values to each element of an array:

It is important to note that the array initializer can only be used when declaring an array variable, and cannot be declared after the array variable.

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

Suppose you initialize an array with curly braces. Also can not know the size of the array, the compiler will actively count the number of elements:

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

another simpler way:

int [] MyArray = {2,3,4,5};

array elements: passing the element number through the indexer. You can access the array; the indexer always starts with 0.

Assuming that the wrong indexer value is used, an exception is thrown IndexOutOfRangeException

declares an array consisting of two objects:

Pulic class person

{public string FirstName;

public string LastName;

}

person [] mypersons = new person[2];

The next thing to note is that the element in the numeric value is a reference type. You must allocate memory for each array element. If an element with unallocated memory in the array is used, an NullReferenceException exception is thrown.

Mypersons[0] = new Person{firstname = "Wang", LastName = "Moumou"};

mypersons[1] = new Person{firstname = "WU", LastName = "Meimei"};

, you can also use the initializer for your own definition type:

person [] MyPerson2 = {

New Person{firstname = "Wang", LastName = "Moumou"},

New person{firstname = "WU", LastName = "Meimei"}

};


2. Multidimensional Arrays:

to declare a two-dimensional array, you need to add a comma to the square brackets; The array should specify the size of each heap at initialization time

int [,] dim2 = new int[3, 3];

dim2[0,0] = 1;

dim2[0,1] = 2;

dim2[0,2] = 3;

dim2[1,0] = 4;

dim2[1,1] = 5;

dim2[1,2] = 6;

dim2[2,0] = 7;

dim2[2,1] = 8;

dim2[2,2] = 9;

Declare a three-dimensional array with two commas in square brackets

3. Jagged array

Like what:

0 S

3 4 5 6

7 8 9 int []

jagged = new int [3][];

Jagged[0]=new int[2]{1,2};

Jagged[1]=new int[4]{3,4,5,6};

Jagged[2]=new int[3]{7,8,9};

Use of arrays in C #

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.