C # Reading Notes (4)-re-learning Array

Source: Internet
Author: User

If you want to use multiple objects of the same type, you can use a set and an array.

1. array declaration and initialization

There are many methods to declare and initialize arrays.

int[] myArray;myArray = new int[4];int[] myArray = new int[4];int[] myArray = new int[4]{2,3,4,5};int[]myArray = new int[]{2,3,4,5};int[]myArray = new {2,3,4,5};

2. Mostly array declarations

int[,] myArray=new int[3,6];//……

Many methods are available for Array initialization.

3. Sawtooth Array


The method for traversing a jagged array is as follows.

int[][] myArray = new int [3][];myArray[0] = new int[2]{3,5};myArray[1] = new int[5]{1,2,3,4,5};myArray[2] = new int[3]{3,4,5};for(int row=0 ; row<myArray.Length ; row++ ){for(int element=0 ; element<myArry[row].Length ; element++ ){//.....}}

4. array class

Using square brackets to create an array is the representation of C # using the array class. When C # syntax is used in the background, a new class derived from the abstract base class array class is created.

4.1 create an array

The array class is an abstract class, so you cannot use constructors to create arrays. You can use the C # syntax and createinstance () to create an array.

Array myArrar = Array.CreateInstance(typeof(int), 5); for (int i = 0; i < 5; i++)    myArrar.SetValue(3 + i, i); for (int i = 0; i < 5; i++)    Console.Write(myArrar.GetValue(i)+" ");

5. array covariant

The array supports covariant, indicating that the array can be declared as the base class.

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.