Sunwen Tutorial----C # Advanced (iii)

Source: Internet
Author: User
Finally to a day, get up, stretch, and sit in front of the computer. What I'm going to tell you today is the C # array (Arrays). Arrays in C #, like some other good languages, are also starting from 0, as we can see in our previous examples, that is, the first element of an array is a[0] instead of a (1) like VB. Although this is true, there are some differences you should pay attention to.

When declaring an array, the square brackets must be followed by the type, not following the variable name, such as:
Int[] table; cannot be written as int table[]
This is obviously different from Java, which is possible in Java.

And in C # you can not specify the size of the array, which is not the same as the C language. This allows you to specify an array of any length, as follows:
Int[] numbers; Its length is arbitrary.
Of course, you can also specify its size:
INT[10] numbers;//Specifies an array of length 10.

In C #, the supported arrays are: single-dimension arrays, multidimensional arrays, and multiple arrays. Their declaration methods are as follows:
Single-Dimension array:
Int[] numbers;
Multidimensional Arrays:
string[,] names;
Multiple arrays:
Byte[][] scores;


Declaring an array does not mean that it has already been established. In C #, all the array elements are objects (pour!), so before you build it, you first instantiate it: &*%$#@.
Single-Dimension array:
int[] numbers = new INT[5];
Multidimensional Arrays:
string[,] names = new string[5,4];
Multiple arrays:


Byte[][] scores = new byte[5][];
for (int x = 0; x < scores. Length; X + +)
{
SCORES[X] = new BYTE[4];
}
Oh, this is a bit strange, do not have to talk about it, later.

We can also create larger arrays, such as a three-dimensional array:
Int[,,] buttons = new int[4,5,3];


We can even mix multidimensional arrays and multiple arrays, and the following examples illustrate these:
int[][,,][,] numbers;

The following example shows all of the above methods of building an array:


From://Arrays\arrays.cs
001:using System;
002:class Declarearrayssample
003: {
004:public static void Main ()
005: {
006://single-dimensional Array
007:int[] numbers = new INT[5];
008:
009://multidimensional array
010:string[,] names = new string[5,4];
011:
012://Array-of-arrays (jagged Array)
013:byte[][] scores = new byte[5][];
014:
015://Create the jagged array
016:for (int i = 0; I < scores. Length; i++)
017: {
018:scores[i] = new BYTE[I+3];
019:}
020:
021://PRint length of each row
022:for (int i = 0; I < scores. Length; i++)
023: {
024:console.writeline ("Length of row {0} is {1}", I, scores[i]. Length);
025:}
026:}
027:}
Its output is:

Length of row 0 is 3
Length of row 1 is 4
Length of row 2 is 5
Length of row 3 is 6
Length of row 4 is 7
The initialization of an array in C # can be initialized at build time, as in Java and C, with {}. Of course, obviously, your initialization value must be the same as the array type you declared, for example, if you define an int type, you can't give it a string, alas, Java looks much, in C #, The string should be written as a string, or else it would be wrong again. Sunwen may have such a mistake in the course of the following, and I hope you can correct me. hehe!

The following example illustrates the initialization of an array:

int[] numbers = new Int[5] {1, 2, 3, 4, 5};
string[] names = new String[3] {"Matt", "Joanne", "Robert"};


You can also omit the size of the array, such as:

int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};

You can even omit the new name if you give the value:


int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Matt", "Joanne", "Robert"};

In C #, the access to the array is the same as the C/c++/java, the following statement establishes an array and assigns its fifth element to a value of 5:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
NUMBERS[4] = 5;

If you do not have c/java/c++ programming experience, then Sunwen here to remind you that numbers[4] represents the fifth element of this array, as I have said earlier, the array is calculated from 0, so 0,1,2,3,4 is exactly fifth, so .... (Under the table: stupid, you think we don't know, go on!)

The following example is about a multidimensional array:

int[,] numbers = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
Numbers[1, 1] = 5;

Again, it is important to note that all arrays in C # are objects (version faint,d), so you can access the array by accessing the object's methods. And System.Array is the abstraction of the array. You can refer to the documentation for the methods supported by the array class. For instance, You can use the Length property to access the lengths of the array. The following example:

int[] numbers = {1, 2, 3, 4, 5};
int lengthofnumbers = numbers. Length;
Oh, OK, another lesson, it's Beijing time 9:16 A.M., I want to rest! Haha, see you later!

The above is the Sunwen tutorial----C # Advanced (three) of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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