| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
|
Arrays
|
|
C# arrays are reference types. The size of the array is not part of the array type
|
|
|
|
Array instances are created using the new keyword. Array elements are default
initialised to zero (enums and numeric types), false (bool), or null (reference types).
|
row = new int[42];
grid = new int[9,6];
|
|
|
Array instances can be initialised:
|
int[] row = new int[4]{ 1, 2, 3, 4 }; // longhand
int[] row = { 1, 2, 3, 4 }; // shorthand
row = new int[4]{ 1, 2, 3, 4 }; // okay
row = { 1, 2, 3, 4 }; // compile time error
|
|
|
Array indexes start at zero and all array accesses are bounds checked
(IndexOutOfRangeException). All arrays implicitly inherit from the System.Array class.
This class brings array types into the CLR and provides some handy properties and
methods:
|
namespace System
{
public abstract class Array : . ..
{
...
public int Length { get { ... } }
public int Rank { get { ... } }
public int GetLength(int rank) { ... }
public virutal IEnumerator GetEnumerator() { ... }
...
}
}
|
|
|
What are Jagged Arrays in C#?
|
|
A special type of array is introduced in C#. A Jagged Array is an array of an array in
which the length of each array index can differ.
|
|
Example: A Jagged Array can be used is to create a table in which the lengths of the
rows are not same. This Array is declared using square brackets ( [ ] ) to indicate each
dimension.
|
|
The following code demonstrates the creation of a two-dimensional jagged array.
|
Class Jagged
{
public static void Main()
{
int [][] jagged=new int [3][];
jagged[0]=mew int[4]
jagged[1]=mew int[3]
jagged[2]=mew int[5]
int I;
‘Storing values in first array
for (I=0;I<4;I++)
jagged[0][I]=I;
‘Storing values in second array
for( I=0;I<3;I++)
jagged[1][I]=I;
‘Storing values in third array
for(I=0;I<5;I++)
jagged[2][I]=I;
‘Displaying values from first array
for (I=0;I<4;I++)
Console.WriteLine(jagged[0][I])
‘Displaying values from second array
for (I=0;I<3;I++)
Console.WriteLine(jagged[1][I])
‘Displaying values from third array
for(I=0;I<5;I++)
Console.WriteLine(jagged[2][I])
}
}
|
|
|
The element type of an array cans itself be an array creating a so-called “ragged” array.
Ragged
arrays are not CLS compliant. We can use a foreach statement to iterate through a ragged
array or through a rectangular array of any rank:
|
class ArrayIteration
{
static void Main()
{
int[] row = { 1, 2, 3, 4 };
foreach (int number in row) {
...
}
int[,] grid = { { 1, 2 }, { 3, 4 } };
foreach (int number in grid) {
...
}
int[][] ragged =
{ new int[2]{1,2}, new int[4]{3,4,5,6} };
foreach (int[] array in ragged) {
foreach (int number in array) {
...
}
}
}
}
|
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords c# passing arrays, c# multidimensional, c# two dimensional array, c# multi dimensional arrays,
c# multidimensional array, c# array, array in c#, c# string array, arrays c#, arrays in c#,
byte array c#, string array in c#, c# dynamic array, c# char array, string to byte array c#,
c# array sort, c# array, c# declare array, c# array declaration, c# initialize array,
array to string c#, c# convert byte array
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|