C # Array Summary

Source: Internet
Author: User

C # Array Summary

  1. Definition of arrays

An array is actually a set of data elements of the same type represented by a variable name.

An array is a reference type.

All elements of an array must be of the same type.

Once the array is created, the size is fixed. C # does not support dynamic arrays.

  2. Array declarations

One-dimensional array declaration: int[] arrary = new int[];

Arrary[0] = 1;

ARRARY[1] = 1;

Or as follows:

int[] arrary = new Int[3] {n-only};

Two-dimensional array declaration: int[,] arrary = new int[,];

int[,] arrary = new Int[,]{{10,1},{11,1},{2,21}}

Jagged array declaration: int[][] arrary = new int[2][];

ARRARY[1] = 1;

ARRARY[2] = 2;

  3. Indexing and initialization of arrays

The index of each dimension in the array starts at 0.

The index inside the square brackets follows the array name.

When an array is created, each element is automatically initialized to the default value of the type. For predefined types, the integer default value is 0, the float default is 0.0, the bool type defaults to False, and the reference type default value is null.

  4. Code examples for multidimensional arrays

var arrary = new int[,]{{0,1,2},{10,11,12}};

for (int i = 0; I <arrary. GetLength (0); i++)

for (int j = 0; J < Arrary. GetLength (1); J + +)
Console.WriteLine ("Arrary[{0},{1}" is {2} ", I, J, Arrary[i, J]); )

An array instance is an object that inherits from System.arrary. The getlength (int n) method is to get the length of the specified dimension in the array.

  5.Clone method

Cloning an array of value types results in 2 independent arrays.

Cloning a reference-type array produces two arrays that point to the same object.

The Clone method returns a reference to the type object, which must be cast to the array type. The code example is as follows:

Int[] Arrary = {n/a};

Int[] arr = (int[]) arrary. Clone ();//must be strengthened to convert (int[])

       

C # Array Summary

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.