Arrays in C# is a more complex topic than in Java. Java has only one category of multi-dimensional array. C# groups multi-dimensional arrays into two distinct categories: 'normal' rectangular arrays, and jagged arrays (see Figure 12.1). Things can be a bit confusing because you can view a Java multi-dimensional array as in either category, though I personally think that it fits a little better into 'rectangular arrays'. Figure 12.1. The different types of arrays in C#.
We will start with the simplest, one dimensional (1D) arrays. Then rectangular multi-dimensional arrays will be covered followed by the more complex jagged multi-dimensional arrays. I shall limit my discussion to 2D arrays for the sections on multi-dimensional arrays “ you can extend the idea to n -levels once you have understood the discussion. This chapter ends with some suggestions on how rectangular and jagged arrays can be used together. I will discuss some general characteristics of arrays in C# first. Like Java
Unlike Java
Additional note C# comes with a new useful looping keyword, foreach , which is very convenient for looping through an array (see section 11.1). |