Arrays
An array is a collection of objects, all of the same type. Arrays are also indexed, meaning that the language provides a way for you to say
"Get me the third item in the array". Indexing also means that the items in the array are stored in a specific order, which further means that you can loop through the
contents of the array in order.
The following are some important general facts about C# arrays:
The following are some important general facts about C# arrays:
- Once an array is created, its size is fixed. C# does not support dynamic arrays.
- Array indexes are 0-based. That is, if the length of a dimension is n, the index values range from 0 to n – 1. For example, following figures shows the dimensions and lengths of two example arrays.
One-dimensional array, int[5]
- Rank: 1
- Length: 5
|
Two-dimensional Array, int[3,6]
- Rank: 2
- Length: 18
|
Types of Arrays
C# provides two kinds of arrays:
- One-dimensional arrays can be thought of as a single line, or vector, of elements.
-
Multidimensional arrays are composed such that each position in the primary vector is itself an array, called a subarray. Positions in the
subarray vectors can themselves be subarrays.
-
Rectangular arrays:
- Are multidimensional arrays where all the subarrays in a particular dimension have the same length
-
Always use a single set of square brackets, regardless of the number of dimensions
int x = myArray2[4, 6, 1] // One set of square brackets
-
Jagged arrays:
- Are multidimensional arrays where each subarray is an independent array
- Can have subarrays of different lengths
-
Use a separate set of square brackets for each dimension of the array
jagArray1[2][7][4] // Three sets of square brackets
-
Rectangular arrays:
Array As an Object
An array instance is an object whose type derives from
Arrays are reference types, and as with all reference types, they have both a reference to the data and the data object itself. The reference is either on the stack or in the heap, and the data object itself is always in the heap.
class System.Array
. Since arrays are derived from this BCL base class,
they inherit a number of useful members from it, such as the following:
Member | Type | Lifetime | Meaning |
---|---|---|---|
Rank |
Property | Instance | Gets the number of dimensions of the array |
Length |
Property | Instance | Gets the total number of elements in all the dimensions of the array |
GetLength |
Method | Instance | Returns the length of a particular dimension of the array |
Clear |
Method | Static | Sets a range of elements to 0 or null |
Sort |
Method | Static | Sorts the elements in a one-dimensional array |
BinarySearch |
Method | Static | Searches a one-dimensional array for a value, using binary search |
Clone |
Method | Instance | Performs a shallow copy of the array—copying only the elements, both for arrays of value types and reference types |
IndexOf |
Method | Static | Returns the index of the first occurrence of a value in a one-dimensional array |
Reverse |
Method | Static | Reverses the order of the elements of a range of a onedimensional array |
GetUpperBound |
Method | Instance | Gets the upper bound at the specified dimension |
Arrays are reference types, and as with all reference types, they have both a reference to the data and the data object itself. The reference is either on the stack or in the heap, and the data object itself is always in the heap.
Instantiating and Assigning Arrays
Once an array is declared, you can immediately fill its values using a comma-delimited list of items enclosed within a pair of curly braces.
Following code is way to instantiating and assigning one-dimensional array:
Following code is way to instantiating and assigning rectangular array:
And here's code for instantiating and assigning jagged array:
string[] languages = { "C#", "COBOL", "Java","C++", "Visual Basic", "Pascal","Fortran", "Lisp", "J#"}; string[] languages = new string[]{"C#", "COBOL", "Java", "C++", "Visual Basic", "Pascal", "Fortran", "Lisp", "J#" }; string[] languages = new string[9]{"C#", "COBOL", "Java", "C++", "Visual Basic", "Pascal", "Fortran", "Lisp", "J#"}; string[] languages; languages = new string[9]; //defining size at runtime Console.WriteLine(languages[1]); //accessing array item
Following code is way to instantiating and assigning rectangular array:
int[,] cells = {{1, 0, 2},{1, 2, 0},{1, 2, 1}}; int[,] cells = new int[,]{ { 1, 0, 2 }, { 1, 2, 0 }, { 1, 2, 1 } }; int[,] cells = new int[3,3]{ { 1, 0, 2 }, { 1, 2, 0 }, { 1, 2, 1 } }; int[,] cells; cells = new [3,3]; //defining size at runtime Console.WriteLine(cells[2, 2]); //accessing array item
And here's code for instantiating and assigning jagged array:
int[][] cells = new int[4][] { new int[] { 1, 0, 2, 0 }, new int[] { 1, 2, 0 }, new int[] { 1, 2 }, new int[] { 1 } }; int[][] cells = { new int[] { 1, 0, 2, 0 }, new int[] { 1, 2, 0 }, new int[] { 1, 2 }, new int[] { 1 } }; int[][] cells; cells = new int[4][]; //defining size at runtime cells[0] = new int[] { 1, 0, 2, 0 }; //defining size at runtime cells[1] = new int[] { 1, 2, 0 }; //defining size at runtime cells[2] = new int[] { 1, 2 }; //defining size at runtime cells[3] = new int[] { 1 } ; //defining size at runtime Console.WriteLine(cells[1][1]); //accessing array item
Rectangular Vs Jagged Arrays
The structure of rectangular and jagged arrays is significantly different. For example, following figure shows the structure of a rectangular three-by-three array,
as well as a jagged array of three one-dimensional arrays of length 3.
The rectangular array has a single array object, while the jagged array has four array objects.
One-dimensional arrays have specific instructions in the CIL that allow them to be optimized for performance. Rectangular arrays do not have these instructions and are not optimized to the same level. Because of this, it can sometimes be more efficient to use jagged arrays of one-dimensional arrays—which can be optimized— than rectangular arrays, which cannot.
On the other hand, the programming complexity can be significantly less for a rectangular array because it can be treated as a single unit, rather than an array of arrays.
One-dimensional arrays have specific instructions in the CIL that allow them to be optimized for performance. Rectangular arrays do not have these instructions and are not optimized to the same level. Because of this, it can sometimes be more efficient to use jagged arrays of one-dimensional arrays—which can be optimized— than rectangular arrays, which cannot.
On the other hand, the programming complexity can be significantly less for a rectangular array because it can be treated as a single unit, rather than an array of arrays.
foreach Statement
The
The important points of the foreach statement are the following:
Or
See following example:
foreach
statement allows you to sequentially access each element in an array. It’s actually a more general construct in that it also
works with other collection types as well.
The important points of the foreach statement are the following:
-
The iteration variable is a temporary variable of the same type as the elements of the array.
The
foreach
statement uses the iteration variable to sequentially represent each element in the array. - The syntax of the foreach statement is shown below
foreach( [Type] [Identifier] in [ArrayName] )
{
....
}
Or
foreach( var [Identifier] in [ArrayName] )
{
....
}
See following example:
static void Main(string[] args) { string[] languages = new string[9] { "C#", "COBOL", "Java", "C++", "Visual Basic", "Pascal", "Fortran", "Lisp", "J#" }; foreach (string language in languages) { Console.WriteLine(language); } }
0 comments:
Post a Comment