C# FAQ: Does CSharp support jagged arrays

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
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];

See also


Personal tools