C # Two-dimensional array definition, initialization (emphasis on understanding the difference between the two)

Source: Internet
Author: User
Tags array definition

After learning C + + basic knowledge, now learning C # is a lot simpler, but still a little different, such as C # two-dimensional array of definitions, declarations are different.


Here's a look at what's different:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Using System.Collections;
            Namespace two-dimensional array {class Program {static void Main (string[] args) {//two-D array:
            Definition and initialization of regular two-dimensional arrays int[,] Arr = new int[2, 5] {1, 2, 3, 5, 6}, {1, 2, 3, 4, 5}};
            Console.WriteLine ("Output of a regular two-dimensional array:");
                    for (int i = 0; i < 2; ++i) {for (int j = 0; j < 5; ++j) {
                Console.Write (Convert.ToString (arr[i,j]) + "");
            } Console.WriteLine ();
            } Console.WriteLine ("----------------");  Irregular two-dimensional array int [] arr = new int [3][];
            Represents an array containing three one-dimensional arrays arr[0] = new int[5]{1,2,3,4,5};
            ARR[1] = new int [2]{0,1};
            ARR[2] = new int[0] {};
            Console.WriteLine ("Output Method One:"); for (int i = 0; i< 2; ++i) {for (int j = 0; j < 5; ++j) {Console.Write (Conv Ert.
                ToString (Arr[i, J]) + "");
            } Console.WriteLine ();
            } Console.WriteLine ();
            Console.WriteLine ("Output Method II:"); for (int II = 0; II < arr. Length; ++II)//arr.
                    Length is 3 to see that arr is an array containing three one-dimensional arrays {foreach (int j in Arr[ii]) {
                Console.Write (j+ "");
            } Console.WriteLine ();


        } console.readkey ();
 }
    }
}


From the above C # code can be seen, C # has two different ways to define two-dimensional arrays, of course, the distinction between the above has been made very clear, do not understand the words copy down to run again, contrast.

and C + + in defining a two-dimensional array, of course, must write out the ' Number of columns 'of the array, and C # 's irregular two-dimensional array can be understood to hold arrays of one-dimensional arrays



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.