The difference between an array in C # and an array in C + +

Source: Internet
Author: User
Tags arrays integer
difference | Arrays in C # are reference types, and C # defines an integer array by: int [] Intarray = {1,2,3}; or int [] Intarray = new int[10]; and in C + +, the integer array is defined as an int intarray[] = {1 , 2,3}; or int * Intarray = new INT[10]; The arrays in C # can be one-dimensional or multidimensional, and also support matrices and jagged arrays. Note: The way to define multidimensional arrays (matrices) is [,,] and the way you define multidimensional "jagged matrices" is []. In addition, the use of the new keyword does not necessarily mean that the object is dynamically allocated (into the stack). The following are common definitions of multidimensional arrays in C # (one-dimensional): int []a1;  //define one-dimensional array int [,]a2; //define two-dimensional array int [,,]a3; Define three-dimensional array int []A1 = new int [10];      //define one-dimensional array depth int [,]A2 = new int [10,20];  &N bsp; //Defines a two-dimensional array depth int [,,]a3 = new int [10,20,30]; Define three-bit array depth int []A1 = new int []{1,2,3};      //init int [,]A2 = new int [,]{{1,2,3},{4,5,6}} You can also define "jagged" arrays: int [][]var = new int [3][];var[0] = new Int[]{1,2,3};var[1] = new Int[]{1,2,3,4,5,6};var[2] = new Int[]{1 , 2,3,4,5,6,7,8,9}; Note that the difference between int [,]a, and int [][]a: The former defines a two-dimensional fixed array, which defines a two-dimensional mutable array. But none of them have been allocated space and initialized. int [][]var = new int[3][4]; it's wrong. The following error: Class test{static void F (int []arr) {}static void Main () {f ({1,2,3});/Error! {1,2,3} is not a valid expression. }} The following is correct: class TEST{STATCI void F (int []arr) {}static void Main () {f (new int []{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.