Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is C # one-dimensional array and multi-dimensional array

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is one-dimensional array and multi-dimensional array of C#". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

C# one-dimensional array and C# multidimensional array

Array is a kind of data structure commonly used in programming. Just like Cpicard +, the array index in C# starts from 0, and its element type must be the same. Of course, the implicit transformation caused by polymorphism is another matter. Arrays in C # can be divided into C # one-dimensional array, C # multi-dimensional array and staggered array (Ragged Array), each of which has different syntax rules for declaration, initialization, and element index. One significant difference between C # and the C++ array is that it is type-safe by the .NET Common language Runtime (CLR) and can be dynamically created and expanded at run time.

C # one-dimensional array declaration and initialization have a specific syntax, and we usually put them together, see the following code:

/ / initialize int [] MyIntArr1=new int [] {1 int [] 3} at the same time of declaration; / / declare and initialize 1 × 3 array int [] MyIntArr2=int {1J 2J 3}; / / declare in simplified form and initialize / / declare and initialize sub-int [] MyIntArr3; MyIntArr3=new int [] {1J 2J 3} / / you cannot initialize the array in a simplified form alone / / you can specify the capacity of the array int [] MyIntArr4=new int [3] {1rem 2je 3}; / / you can't: int [3] MyIntArr4=new int [3] {1m 2je 3}

Note that the parenthesis ([]) in the declaration section here comes immediately after the array element type (such as int), followed by the variable name. C# does not support the traditional C++ declaration of "int a [] = {1pjol 2pyrm 3pyrm 4pr 5}". To the right of the equal sign is the initialization part. We can see that C# supports two initialization expressions, of which the latter is a simplification of the former, but when the declaration is separated from the initialization, the simplified initialization can no longer be used. The initialization part of the C # array can specify the capacity of the array in parentheses ([]) (where it must be an integer constant or constant, of course, it must match the number of elements of the array that are initialized later), and the array declaration part cannot contain the capacity of the array.

C # supports C# multidimensional arrays, and its initialization is expressed in nested curly braces like C++, but its declaration is different from C++. See the following code and comments:

/ / initialize int [,] MyMulArr1=new int [,] {{1int [,] MyMulArr3; MyMulArr3=new int [,] {{1rec 2pr 3}, {2pr 4pm 6}}; / / declare and initialize the 2 × 3 array int [,] MyMulArr2= {{1rec 2jue 3}, {2pm 4pm 6}}; / / simplified initialization / / statement and initialization sub-int [,] MyMulArr3; MyMulArr3=new int [,] {{1pm 2pr 3}, {2pm 4pm 6}} / / the capacity of the array can be specified in initialization int [,] MyMulArr4=new int [2pje 3] {{1rec 2je 3}, {2pr 4pm 6}}; / / No: int [2dhom 3] MyMulArr5=new int [2pr 3] {{1m 2J 3e 3}, {2je 4pm 6}}

C # declares C # multidimensional arrays with commas (,) in parentheses, and multiple parentheses (such as int [] [] a) are used to represent staggered arrays in C #. The declaration and initialization of C# multidimensional arrays are similar to those of C# one-dimensional arrays, as we can see from the example above.

This is the end of the content of "what is one-dimensional array and multi-dimensional array of C#". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report