C# array
What is an array?
An array is a data structure that contains multiple elements of the same type.
Declaration of array:
int[] myIntArray;
Note: When declaring an array, square brackets ([]) must follow the type, not the variable name. In C#, it is illegal syntax to put square brackets after a variable name.
Initialization of array:
We know that array is a reference type, so we need to allocate memory on the heap to it.
1.myIntArray = new int[3];
2.myIntArray = new int[] { 1, 2, 3 };
3.int[] myIntArray = { 1, 2, 3 }; //When using this When the method initializes the array, it can only be used when declaring the variable array, not after declaring the array.
Array access:
After the array is declared and initialized, it can be accessed using an indexer. The indexer always starts with 0, indicating the first element.
int[] myIntArray = { 1, 2, 3 }; Console.WriteLine("intValue = {0}", myIntArray[0]); Console.Read();
The result is: intValue = 1
Type of array:
1. Multi-dimensional array:
General arrays (also called one-dimensional arrays) are indexed by an integer, and multi-dimensional arrays are indexed by two or more integers.
static void Main(string[] args) { //声明一个二维数组 两行三列 int[,] myIntArray1; myIntArray1 = new int[2, 3]; myIntArray1[0, 0] = 1; myIntArray1[0, 1] = 11; myIntArray1[0, 2] = 111; myIntArray1[1, 0] = 2; myIntArray1[1, 1] = 22; myIntArray1[1, 2] = 222; Console.WriteLine("{0},{1},{2}", myIntArray1[0, 0], myIntArray1[0, 1], myIntArray1[0, 2]); Console.WriteLine("{0},{1},{2}", myIntArray1[1, 0], myIntArray1[1, 1], myIntArray1[1, 2]); Console.Read(); }
The result is:
static void Main(string[] args) { //声明一个二维数组 三行三列 int[,] myIntArray2; myIntArray2 = new int[,] { { 1, 11, 111 }, { 2, 22, 222 }, { 3, 33, 333 }, { 4, 44, 444 } }; Console.WriteLine("{0},{1},{2}", myIntArray2[0, 0], myIntArray2[0, 1], myIntArray2[0, 2]); Console.WriteLine("{0},{1},{2}", myIntArray2[1, 0], myIntArray2[1, 1], myIntArray2[1, 2]); Console.WriteLine("{0},{1},{2}", myIntArray2[2, 0], myIntArray2[2, 1], myIntArray2[2, 2]); Console.WriteLine("{0},{1},{2}", myIntArray2[3, 0], myIntArray2[3, 1], myIntArray2[3, 2]); Console.Read(); }
The result is:
static void Main(string[] args) { //声明一个三维数组 三行三列 int[, ,] myIntArray3; myIntArray3 = new int[,,] { { {1,1}, {11,11}, {111,111} }, { {2,2}, {22,22}, {222,222} }, { {3,3}, {33,33}, {333,333} }, { {4,4}, {44,44}, {444,444} } }; Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[0, 0, 0], myIntArray3[0, 0, 1], myIntArray3[0, 1, 0], myIntArray3[0, 1, 1], myIntArray3[0, 2, 0], myIntArray3[0, 2, 1]); Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[1, 0, 0], myIntArray3[1, 0, 1], myIntArray3[1, 1, 0], myIntArray3[1, 1, 1], myIntArray3[1, 2, 0], myIntArray3[1, 2, 1]); Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[2, 0, 0], myIntArray3[2, 0, 1], myIntArray3[2, 1, 0], myIntArray3[2, 1, 1], myIntArray3[2, 2, 0], myIntArray3[2, 2, 1]); Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[3, 0, 0], myIntArray3[3, 0, 1], myIntArray3[3, 1, 0], myIntArray3[3, 1, 1], myIntArray3[3, 2, 0], myIntArray3[3, 2, 1]); Console.Read(); }
The result is:
2. Sawtooth array:
Generally, the size of multi-dimensional arrays is rectangular, and the size of sawtooth arrays is relatively small Flexible, each row can be a different size.
When initializing the sawtooth array, first set the number of rows contained in the array. The second bracket defining the number of elements in each row is set to empty because each row of such an array contains a different number of elements.
static void Main(string[] args) { //声明一个锯齿数组 三行 int[][] myIntArray4; myIntArray4 = new int[3][]; myIntArray4[0] = new int[] { 1,11,111}; myIntArray4[1] = new int[2] { 2, 22 }; myIntArray4[2] = new int[] { 3,33,333,3333}; for (int i = 0; i < myIntArray4.Length; i++) { for (int j = 0; j < myIntArray4[i].Length; j++) { Console.WriteLine("{0}",myIntArray4[i][j]); } } Console.Read(); }
The result is:
Array in C#
For more articles related to array in C#, please pay attention to the PHP Chinese website!

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



Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

How to use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...
