Beginner with c #5

Source: Internet
Author: User

1. 5 Array type (Array types)

Arrays can be one-dimensional or multi-dimensional. The members of the ancestor can be neat or long.

One-dimensional arrays are the most common and simple. Here is an example, so we will not explain it much. */
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]);
}
}

/* The result is as follows:
Arr [0] = 0
Arr [1] = 1
Arr [2] = 4
Arr [3] = 9
Arr [4] = 16

We can also compare the definitions and values of multi-dimensional, rule, and variable-length arrays :*/
Class Test
{
Static void Main (){
Int [] a1 = new int [] {1, 2, 3}; // One-dimensional
Int [,] a2 = new int [,] {1, 2, 3}, {4, 5, 6}; // two-dimensional
Int [,] a3 = new int [10, 20, 30]; // 3D
Int [] [] j2 = new int [3] []; // Variable Length
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 arrays of various styles. The a1, a2, and a3 variables are rule arrays. J2 is a variable-length array.
The rule array can easily calculate their length. For example, the a3 length is 10*20*30 = 6000. On the contrary, variable length
Arrays are a little different, and each dimension must be defined separately. For example, the first dimension of j2 is 3, the second dimension is 6
The total length is: 1*3 + 1*6 + 1*9 = 18.

Array assignment is a rigorous style. In some cases, we can simplify the writing, but I always think this simplification.
Too many application restrictions and error-prone. I will not introduce it here. Here is another example to illustrate how the parameters in the function are
Assign value */
Class Test
{
Static void F (long [] arr ){}
Static void Main (){
F (new longt [] {1, 2, 3 });
}
}

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.