Basic knowledge of C #: Basics (14) arrays

Source: Internet
Author: User
No matter what language, there will be a collection of concepts. The simplest, most intuitive collection should be an array, an array of contiguous spaces in memory. Look at the array in C #

The definition.
1, int[] intarry;
intarry= New Int[6];
This declares an int array type variable intarry, and holds an int array object with 6 cells;
int[,] IntArry2 = new int[3, 4];
declares an int two-dimensional array type variable and initializes an array object with 3 rows and 4 columns;
int[][] IntArry3 = new int[9][];
Declares an array variable of an array cell of type int, each array element is an object reference of an int array type.
Because it is an object-oriented language, references and objects are mentioned above. In fact:
1. NET Frameword array is not a simple data structure, but a type, called an array type;
2. An array variable in the. NET Framework holds a reference to an array-type object, which means that the array is an object.
All. NET Framework arrays (int[],string[],object[]) are subclasses that inherit from an array. The array class is not typically used directly because the. NET Framework

Languages, of course including C #, map array objects to their own special syntax, such as int[],string[].
The array has two main properties: Index and length, which is index and lengths, which are used to access the elements in the array object, and the length is the length of the array.
Take a look at the contact code:

public class MyArray {//<summary>//define array Test///</summary> public void test            Int () {int[] intArry1 = null;            IntArry1 = new Int[6];            int[,] IntArry2 = new int[3, 4];        int[][] IntArry3 = new int[9][]; }///<summary>///value type array to reference type array test///</summary>//<param name= "Arra Y "></param>///<returns></returns> public static object[] Int32toarrayofobject (int[] A Rray) {object[] Objarray = new Object[array.            Length]; for (int i = 0; i < array. Length;            i++) {Objarray[i] = Array[i];        } return Objarray;        }///<summary>///array main characteristics Test///</summary> public static void Maintest ()            {//declares a string array containing an element string[] Sarray = new STRING[10]; accessing Arrays//Assignment for (int i = 0; i < sarray.length; i++) {Sarray[i] = @ "string" + I;            } consoletoclientstring (Sarray);            Another way to declare an array, the so-called enumeration method Sarray = new string[] {"TESTSTRING0", "TestString1", "TestString2"};            Consoletoclientstring (Sarray);            Array copy string[] Newsarray = Sarray.clone () as string[];            Consoletoclientstring (Newsarray);            Use the CreateInstance method of the array to declare an array of 10 elements int[] Intarray = Array.CreateInstance (typeof (int), ten) as int[];            for (int i = 0; i < intarray.length; i++) {intarray[i] = i;            } consoletoclientint (Intarray);            Copy between arrays, specify position, specified length int[] Newintarray = new INT[20];            Array.copy (Intarray, 3, Newintarray, 4, intarray.length-3);            Consoletoclientint (Newintarray);            object[] Objarray = Sarray;  Consoletoclientobject (Objarray);          Objarray = Int32toarrayofobject (Intarray);            Consoletoclientobject (Objarray);            Array of arrays int[][] Intarrayarray = new int[9][];            Console.WriteLine ("Array Length:" + intarrayarray.length);                Assignment for (int i = 1; i < i++) {intarrayarray[i-1] = new Int[i];                for (int j = 1; J <= I; j + +) {Intarrayarray[i-1][j-1] = i * j;                        }} consoletoclientarrayarrayint (Intarrayarray);            Two-dimensional array int[,] intarray2d = new int[9, 9]; Console.WriteLine (String.            Format ("Two-dimensional array length: {0}, Dimension: {1}*{2}", Intarray2d.length, Intarray2d.getlength (0), Intarray2d.getlength (1)));                    for (int i = 1; i <, i++) {for (int j = 1; J <= I; j + +) {                Intarray2d[i-1, j-1] = i * j; }} int count = 0;           foreach (int item in intarray2d) {if (item > 0) {                Console.Write ("{0,2}", item);                    } if (++count >= 9) {Console.WriteLine ();                Count = 0;  }}}} static void Consoletoclientarrayarrayint (int[][] intarrayarray) {foreach            (int[] item1 in Intarrayarray)                {foreach (int item2 in item1) {Console.Write ("{0,2}", item2);            } Console.WriteLine ();        } Console.WriteLine ();            } static void Consoletoclientstring (string[] sarray) {foreach (string item in Sarray)            {Console.Write (item + @ ",");        } Console.WriteLine (); } static void Consoletoclientint (int[] intarray) {foreach (inT item in Intarray) {Console.Write (item + @ ",");        } Console.WriteLine ();            } static void Consoletoclientobject (object[] objarray) {foreach (object item in Objarray) {Console.Write (item.            ToString () + @ ",");        } Console.WriteLine (); }    }

Call

    Class program    {        static void Main (string[] args)        {            myarray.maintest ();            Console.ReadLine ();        }    }


Results


From the above can be known:
Arrays have reference-type arrays and value-type arrays, and for reference-type arrays, elements are used to hold references to objects, initialization values are null, and for value-type arrays, elements are saved

The value of the object, for numeric type initialization value of 0.
Arrays have dimensions, but multidimensional arrays and arrays are different concepts, and Intarrayarray and intarray2d are different. An array of arrays represents a m*n determinant

, a multidimensional array is an array in which each element is an array object.
arrays, like other collection classes, implement the ICollection interface, with both enumeration and iteration capabilities.

The above is the basic knowledge of C #: Basic knowledge (14) The contents of the array, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.