Arrays in C #

Source: Internet
Author: User
Tags arrays

We need to use an array when we're working on the batch data. An array is an ordered set of data of the same type. Arrays are described by array names, the type of data elements, and the number of dimensions. The System.Array class provided in C # is the base class for all array types.

The declared format of the array:

non-array-type[Dim-separators] Array-instance name;

For example, we declare an array of integers:

Int[] arr;

When defining an array, you can specify the number of elements in the array in advance, and the number of elements in the array is defined in "[]", which can be obtained by adding a dot to the array name plus "Length". When using an array, you can add a subscript to the "[]" to get the corresponding array element. The subscript for an array element in C # starts at 0, that is, the first element corresponds to a subscript of 0, and then increases one after the other.

Arrays in C # can be one-dimensional or multidimensional, and also support matrices and jagged arrays. One-dimensional arrays are the most common and most commonly used. Let's take a look at the following example:

Program Listing 4-3:

Using System:
class Test
{
  static void Main () {
    int[] arr=new int[5];
    for (int i=0;i〈arr. length;i++)
       arr[i]=i*i;
    for (int i=0;i〈arr. length;i++)
       Console.WriteLine ("Arr[{0}]={1}", I,arr[i]);
  }

This program creates a one-dimensional array with a base type of int, which is initialized and then output by item. Which arr. Length represents the number of array elements. The output of the program is:

Arr[0]=0
Arr[1]=1
Arr[2]=4
Arr[3]=9
Arr[4]=16

In the example above we use a one-dimensional, very simple! Here we introduce the multidimensional:

Class Text
{
  static void Main () {      //can dynamically generate the length of an array
     string[] A1;  One-dimensional string array
     string[,] A2  //Two D
     string[,,] A3//three-dimensional
     string[][] J2;  Variable group (array)
           string[][][][] J3;//multidimensional mutable array
}

Arrays can be assigned to an array element when they are declared, or they are called an initialization of a logarithmic group. You can also dynamically assign values when you use them. Look at the following example:

Class Test
{
  static void Main () {
    int[] a1=new int[] {1,2,3};
    int[,] a2=new int[,] {{1,2,3},{4,5,6}};
    Int[,,] a3=new int[10,20,30];
    Int[][] J2=new int[3][];
    J2[0]=new int[] {1,2,3};
    J2[1]=new int[] {1,2,3,4,5,6};
    J2[2]=new int[] {1,2,3,4,5,6,7,8,9};
   }
The above example shows that array initialization can be in several different types, because it requires the type to be determined at the time of initialization. For example, the following wording is wrong:

class Test
{
  static void F (int[] arr) {}
  static void Main () {
    F ({1,2,3});
  }
}
{1,2,3} is not a valid expression because the array is initialized. We have to be clear about the array type:

class Test
{
  static void F (int[] arr) {}
  static void Main () {
     F (new int[] {1,2,3}) ;
  }
}

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.