The use and creation of C # arrays

Source: Internet
Author: User

Definition of the array:

An array is a set of elements of the same data type arranged in a certain order, that is, a variable of the same type is named with a name, and then the set of the variable is distinguished by a number, which is called the array name, and the number is called the subscript. Each variable that makes up an array is called the component of an array, also known as an element of an array, sometimes called a subscript variable. An array is a form in which a number of variables of the same type are organized in an orderly manner in order to deal with them conveniently. The collection of these ordered, homogeneous data elements is called an array.

Array has the properties:

    1. An array can be one-dimensional, multidimensional, or interleaved.
    2. When an array instance is created, the dimensions and the length of each dimension are established. These values cannot be changed during the lifetime of the instance.
    3. The default value of a numeric array element is set to zero, and the default value of the reference element is set to NULL.
    4. A jagged array is an array of arrays, so its elements are reference types and are initialized to null.
    5. The index of the array is zero-based: The index of an array with n elements is from 0 to n-1.
    6. An array element can be any type, including an array type.
    7. An array type is a reference type derived from an abstract base type array. Because this type implements IEnumerable and Ienumerable<t>, you can use foreach iterations for all arrays in C #.

To define an array:

1. First define the number of groups

Int[] A = new Int[length];//int

String[] A = new string[length];//string

list<string[]> li = new list<string[]> ();//array collection (the array collection is different from the normal array, it has no length, does not need a length, how much data can be loaded)

Note: Length indicates the size of the array (the capacity of the array)

//一维数组

private static void Array1() {      //声明包涵5个整数的一维数组      int [] array =  new int [5];      //通过foreach遍历数组,为数组赋值      foreach ( int item  in array)      {          //给数组赋值          array[item] = item * 2;      } } //多维数组 private static void Array2() {      //声明一个3行4列的二维数组      int [,] array =  new int [3, 4];      int value = 0;      //给数组赋值      //数组.GetLength(i)获取数组i维度的长度      //数组.Rank获取数组的维度,比如array.Rank 的值为2      for ( int i = 0; i < array.GetLength(0); i++)      {          for ( int n = 0; n < array.GetLength(1); n++)          {              array[i, n] = value;              value++;          }      } } //交错数组 //交错数组是元素为数组的数组。交错数组元素的维度和大小可以不同。 //交错数组有时称为“数组的数组”。 private static void Array3() {      //声明一个由三个元素组成的一维数组,其中每个元素都是一个一维整数数组:      int [][] array =  new int [2][];      //必须初始化 array 的元素后才可以使用它。可以如下例所示初始化该元素:      array[0] =  new int [2];      array[1] =  new int [1];      array[0][0] = 1;      array[0][1] = 2;      array[1][0] = 3; }

Convenient mode for arrays:

1. Use for loop convenience

for (int i = 0; i < A.count (); i++)
{
Define a variable to accept the value of the array = A[i];
}

2. Use the Foreach Loop to facilitate

foreach (var item in a)
{
Define a variable to accept the value of the array = Item;
}

......

  

The use and creation of C # arrays

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.