C# array

Dec 16, 2016 pm 02:06 PM
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();
Copy after login

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();
        }
Copy after login

The result is:

C# array

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();
        }
Copy after login

The result is:

C# array

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();
        }
Copy after login

The result is:

C# array

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.

C# array

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();
        }
Copy after login

The result is:

C# array

Array in C#

For more articles related to array in C#, please pay attention to the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

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

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

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

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

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

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

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

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

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

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

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

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

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 restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? How to restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? Apr 19, 2025 pm 02:30 PM

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

See all articles