C# FAQ: Does CSharp support jagged arrays
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
[edit]
Does C# support jagged arrays?
In both the C and C++ languages, each subarray of a particular multi-dimensional array must have identical dimensions. In other words, arrays must be orthogonal. However, in both the Java and C# languages, arrays need not be orthogonal; because, arrays are constructed as arrays of arrays.
In C#, each array is one-dimensional. Therefore, jagged arrays of varying sizes can be built. The contents of a jagged array is arrays of instances or of references to arrays. Therefore, the rows and columns of a jagged array need not be of uniform length.
The following C# example illustrates how to construct a jagged array—the code works in either the Java or C# language:
int [][]anArray = new int[3][]; anArray [0] = new int[7]; anArray [1] = new int[13]; anArray [2] = new int[5];
[edit]
See also
- C# for Java Developers
- Does C# check array bounds? (array bounds checking)
- Does C# support jagged arrays?
- Should calling
Initialize()on a reference type array fill it with objects?