| |
|
Arrays in C#
|
| |
|
I always liked the concept of arrays. Being able to conceptually visualize in
multiple dimensions took me some time to get my mind around, but in the end I
was successful. The example here will discuss 2 dimensional arrays of type
single, multiple and jagged.
|
| |
|
A single dimensional array is the simplest. You simply code an object 1 column
by (n) rows. Like this image and code represent:
|
| |
int[] singleDimensionalArray = new int[2];
|
| |
 |
| |
| The single dimension array is of type integer with a single column
and [2] rows defined. Take note that the numbering of arrays always starts with 0
and not with one. If you wanted to access the value stored in the first row you
would use;
|
| |
int firstRow = singleDimensionalArray[0];
|
| |
| The second type of array is a multi dimensional array. It supports
(n) number of columns and (n) number of rows. You can think of this as cells
created in a spreadsheet. Like in this image:
|
| |
 |
| |
| And this code:
|
| |
int[,] multiDimensionalArray = new int[2, 2];
|
| |
| Again, I coded this one of type integer with a structure of 2 by 2,
I.e. [2, 2]. This means I have a table 2 columns wide by 2 rows deep. If you
needed to access the value (210) stored in [1, 0] the following would provide it:
|
| |
int multValue = multiDimensionalArray[1, 0]
|
| |
| The final type of array discussed here is called a Jagged array.
This array allows you to have arrays embeded within arrays of different length
of a single type.
|
| |
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[7];
|
| |
 |
| |
| To access the value 501 stored at location [1][4] you would used this code:
|
| |
int jaggedValue = jaggedArray[1][4];
|
| Download the source |
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
| Posts: 113 |
| Comments:
86 |
| Fundamentals:
16 |
| |
 |
| |
| |
|
| |
 |
| |
 |
| |
 |
| |
|
| 2011 December (2) |
| 2011 November (6) |
| 2011 October (7) |
| 2011 September (7) |
| 2011 August (9) |
| 2011 July (9) |
| 2011 June (8) |
| 2011 May (9) |
| 2011 April (7) |
| 2011 March (9) |
| 2011 February (8) |
| 2011 January (8) |
| 2010 December (7) |
| 2010 November (8) |
| 2010 October (4) |
| |
| |
|
| |
|
|
| |
| |
|
The sample code on this website is provided to illustrate a concept and should not be used in
applications or Web sites without proper professional consultation, as it may not illustrate
the safest coding practices. I assume no liability for incidental or consequential damages
should the sample code be used for purposes other than as intended.
|
| |
|
| | | |