Home  >  Article  >  Backend Development  >  SUNWEN tutorial - C# advanced (3)

SUNWEN tutorial - C# advanced (3)

黄舟
黄舟Original
2016-12-19 10:18:101148browse

It’s finally daytime again. I got up, stretched, and sat in front of the computer again. Today I want to talk to you about C#’s arrays (Arrays). Arrays in C#, like some other excellent languages, also originate from Starting from 0, this can be seen from our previous examples, that is to say, the first element of an array is a[0], not a(1) like VB. Although this is the case, you still Pay attention to some differences.

When declaring an array, square brackets must follow the type, not the variable name, such as:
int[] table; //It cannot be written as int table[]
This is obvious It is different from java. This is possible in JAVA.

Also, in C# you can not specify the size of the array, which is different from 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 with a length of 10.

In C#, Supported arrays include: single-dimensional arrays, multi-dimensional arrays and multiple arrays. Their declaration methods are as follows:
Single-dimensional array:
int[] numbers;
Multi-dimensional array:
string[,] names;
Multiple array:
byte[ ][] scores;


Declaring an array does not mean that it has been created. In C#, all array elements are objects (really! How can it be the same as JAVA&*%$#@), so when creating Before doing it, you must first instantiate it:
Single-dimensional array:
int[] numbers = new int[5];
Multi-dimensional array:
string[,] names = new string[5,4];
Multiple array:


byte[][] scores = new byte[5][];
for (int x = 0; x < scores.Length; x++)
{
scores[x] = new byte[4];
}
Haha, this is a bit strange, don’t worry about it now, we will talk about it later.

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


We can even mix multidimensional arrays and multiarrays, the following example illustrates these:
int[][,,][,] numbers;

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


000: // Arraysarrays.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
in C# The initialization of the array can be initialized when it is created, just like JAVA and C, using {}. Of course, it is obvious that your initialization value must be the same as the array type you declare. For example, if you define an int type, you You can't give it a String. Alas, I have read too much about JAVA. In C#, String should be written as string, otherwise, another error will occur. SUNWEN may make such errors in subsequent courses, and I hope everyone can correct me. Haha !

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 slang name if you give it a value:


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

In C#, array access is the same as C/C++/JAVA. The following statement establishes Create an array and assign its fifth element to 5:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4 ] = 5;

If you have no programming experience in C/JAVA/C++, then SUNWEN would like to remind you that numbers[4] represents the fifth element of this array, because as I said before, the array is Counting starts from 0, so 0,1,2,3,4 happens to be the fifth one, so.... (Audience: Idiot, you think we don’t know, go on!)

The following example It’s about multi-dimensional arrays:

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

Note again that all arrays in C# are objects (faint, D version), so you can access the array using the method of accessing the object. And System.Array is the abstraction of the array. You You can refer to the documentation to see the methods supported by the Array class. For example, you can use the length attribute to access the length of the array. For example:

int[] numbers = {1, 2, 3, 4, 5} ;
int LengthOfNumbers = numbers.Length;
Oh, okay, another lesson is over, it’s 9:16 am Beijing time, I’m going to take a break! Haha! See you later!

The above is the content of SUNWEN tutorial - C# Advanced (3). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn