SUNWEN tutorial - C# advanced (3)
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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages such as Python. Be careful when modifying and back up the original files.
