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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor