C # Learning diary 16 --- array of reference types

Source: Internet
Author: User

C # Learning diary 16 --- array of reference types

The definition of the array is already in C # Learning diary 12 --- reference typeAs mentioned in, here we will not be arrogant. In general, the array is a group of ordered data of the same type, System. the Array class is the base class of all arrays, such as System. object is the same as the base class of all classes ).

Declare an array:

Int [] arr; // here we declare an array, which is not initialized and cannot be used. For example, if arr [0] = 1, an error is returned.

Since I used to write the C-defined array method, I often define int arr [] When writing C #; this writing is illegal for C.

 

Initialize the array:

After the above steps, we have successfully declared a one-dimensional array. The arrays in C # can be one-dimensional or multi-dimensional, and also support arrays and uneven arrays, as a beginner, we should first learn from the one-dimensional array, which will be followed by some multidimensional ones.

Array Initialization is essentially to open up storage space for the array and assign the initial value (the default initial value is 0). Of course, you can also assign the specified value to the array during initialization. Because arrays are of reference type, you need to use the new statement to create an array instance:

Arr = new int [5];

The number of array elements (5) is defined in '[]' during initialization. length to access arry. length; the subscript corresponding to the first element of the array is 0, followed by + 1.

 

Assign a value to the array:

After the above two steps, we have successfully created a one-dimensional array with a space of 5 * size (int) and assigned the initial value 0 to the elements in it. For example, we output the second element in arr. Console. WriteLine (arr [1]); (the first element subscript corresponds to 0, and the second element corresponds to 1 ):

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {static void Main (string [] args) {int [] arr; // declare the array arr = new int [5]; // initialize int I = arr. length; // obtain the number of elements. writeLine (value: {0} Length: {1}, arr [1], I); // output the value of the second element and number of elements }}}


The result is as follows:

 

Here, the value of arr [1] is the default value 0. There are two ways to assign a value to a human object:

 

1. Actively assign values during initialization;

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {static void Main (string [] args) {int [] arr; // declare the array arr = new int [5] {1, 2, 3, 4, 5}; // initialize and assign the value int I = arr. length; // obtain the number of elements. writeLine (value: {0} Length: {1}, arr [1], I); // output the value of the second element and number of elements }}}

In fact, you can perform the following operations during initialization:

Int [] arr = {1, 2, 3, 4, 5} // you do not need to write a large or small value.

 

2. Access each element and change the default value;

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {static void Main (string [] args) {int [] arr; // declare the array arr = new int [5]; // initialize and assign int I = arr. length; // obtain the number of elements // access one by one and assign values to 1, 2, 3, 4, 5 for (int j = 0; j
 
  

 

Foreach () Statement:

In the above example, we use a for loop to access each array element and assign values. Now I want to useForeachStatement to traverse the array and output all results. The foreach statement is used to access the set cyclically to obtain the information you need, but cannot be used to add or remove items in the source set. Otherwise, unpredictable side effects may occur. To add or remove items in the source set, use the for loop.

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {static void Main (string [] args) {int [] arr; // declare the array arr = new int [5]; // initialize and assign int I = arr. length; // obtain the number of elements // access one by one and assign values to 1, 2, 3, 4, 5 for (int j = 0; j
   
    

Running result:

 

 

 

Here is a simple introduction to the usage of one-dimensional arrays. There will also be learning about multi-dimensional arrays, staggered arrays, parameter arrays, and arrays, your comments and suggestions are my motivation to improve myself ...... Pai_^ ...... Thank you ......

 

 

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.