C # syntax exercise (7): Array

Source: Internet
Author: User
String Array:
 
Using system; Class myclass {static void main () {string [] arr = new string [3] {"AA", "BB", "cc "}; foreach (string s in ARR) console. writeline (s); // AA/BB/CC console. readkey ();}}

  

Integer array:

 
Using system; Class myclass {static void main () {int [] arr = {11, 22, 33}; foreach (int I in ARR) console. writeline (I); // 11/22/33 console. readkey ();}}

  

The number of dimensions can be omitted during initialization. If not, the values must be consistent:

 
Using system; Class myclass {static void main () {int [] arr = new int [3] {11, 22, 33}; foreach (int I in ARR) console. writeline (I); // 11/22/33 console. readkey ();}}

  

The statement specifies the dimension at the same time, but does not assign a value for the moment:

Using system; Class myclass {static void main () {int [] arr = new int [3]; foreach (int I in ARR) console. writeline (I); // 0/0/0 arr [0] = 11; arr [1] = 22; arr [2] = 33; foreach (int I in ARR) console. writeline (I); // 11/22/33 console. readkey ();}}

  

Declare the dimension before determining the dimension when assigning values:

 
Using system; Class myclass {static void main () {int [] arr; arr = new int [] {11, 22, 33}; foreach (int I in ARR) console. writeline (I); // 11/22/33 console. readkey ();}}

  

The dimension of the statement can be changed:

 
Using system; Class myclass {static void main () {int [] arr = new int [3]; arr = new int [4] {11, 22, 33, 44 }; foreach (int I in ARR) console. writeline (I); // 11/22/33/44 console. readkey ();}}

  

If the variable is used as the array dimension, it must be const:

 
Using system; Class myclass {static void main () {const int size = 3; int [] arr = new int [size] {11, 22, 33 }; foreach (int I in ARR) console. writeline (I); // 11/22/33 console. readkey ();}}

  

Two-dimensional array initialization:

 
Using system; Class myclass {static void main () {int [,] arr = {11, 12, 13, 14}, {,}, {, 34 }}; foreach (int I in ARR) console. writeline (I); console. readkey ();}}

  

Two-dimensional array assignment:

Using system; Class myclass {static void main () {int [,] arr = new int [3, 4]; arr [0, 0] = 11; arr [0, 1] = 12; arr [] = 13; arr [] = 14; arr [] = 21; arr [] = 22; arr [] = 23; arr [] = 24; arr [] = 31; arr [] = 32; arr [] = 33; arr [] = 34; foreach (int I in ARR) console. writeline (I); console. readkey ();}}

  

Multi-dimensional array:

 
Using system; Class myclass {static void main () {int [,] arr = new int [2, 3, 4]; for (INT x = 0; x

Array:

Using system; Class myclass {static void main () {int [] [] arr = new int [3] []; arr [0] = new int [2] {11, 12}; arr [1] = new int [3] {21, 22, 23}; arr [2] = new int [4] {31, 32, 33, 34}; foreach (INT [] ns in ARR) foreach (int n in NS) console. writeline (n); console. readkey ();}}

  

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.