Table of Contents
# Multidimensional Arrays
string [,] names;
Example
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
/* an array with 5 rows and 2 columns*/
int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
int i, j;
/* output each array element's value */
for (i = 0; i < 5; i++) {
for (j = 0; j < 2; j++) {
Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
}
}
Console.ReadKey();
}
}
}
=> output
a[0,0]: 0 a[0,1]: 0 a[1,0]: 1 a[1,1]: 2 a[2,0]: 2 a[2,1]: 4 a[3,0]: 3 a[3,1]: 6 a[4,0]: 4 a[4,1]: 8
# Jagged Arrays
int [][] scores;
Example
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
/* a jagged array of 5 array of integers*/
int[][] a = new int[][]{new int[]{0,0},new int[]{1,2},
new int[]{2,4},new int[]{ 3, 6 }, new int[]{ 4, 8 } };
int i, j;
/* output each array element's value */
for (i = 0; i < 5; i++) {
for (j = 0; j < 2; j++) {
Console.WriteLine("a[{0}][{1}] = {2}", i, j, a[i][j]);
}
}
Console.ReadKey();
}
}
}
=> output
a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8
# Passing Arrays as Function Arguments
using System;
namespace ArrayApplication {
class MyArray {
double getAverage(int[] arr, int size) {
int i;
double avg;
int sum = 0;
for (i = 0; i < size; ++i) {
sum += arr[i];
}
avg = (double)sum / size;
return avg;
}
static void Main(string[] args) {
MyArray app = new MyArray();
/* an int array with 5 elements */
int [] balance = new int[]{1000, 2, 3, 17, 50};
double avg;
/* pass pointer to the array as an argument */
avg = app.getAverage(balance, 5 ) ;
/* output the returned value */
Console.WriteLine( "Average value is: {0} ", avg );
Console.ReadKey();
}
}
}
=> output
Average value is: 214.4
# Param Arrays
using System;
namespace ArrayApplication {
class ParamArray {
public int AddElements(params int[] arr) {
int sum = 0;
foreach (int i in arr) {
sum += i;
}
return sum;
}
}
class TestClass {
static void Main(string[] args) {
ParamArray app = new ParamArray();
int sum = app.AddElements(512, 720, 250, 567, 889);
Console.WriteLine("The sum is: {0}", sum);
Console.ReadKey();
}
}
}
=> output
The sum is: 2938
# Array Class
Properties
IsFixedSize | Gets a value indicating whether the Array has a fixed size. |
IsReadOnly | Gets a value indicating whether the Array is read-only. |
Length | Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array. |
LongLength | Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array. |
Rank | Gets the rank (number of dimensions) of the Array. |
Methods
Clear | Sets a range of elements in the Array to zero, to false, or to null, depending on the element type. |
Copy(Array, Array, Int32) | Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer. |
CopyTo(Array, Int32) | Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer. |
GetLength | Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array. |
GetLongLength | Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array. |
GetLowerBound | Gets the lower bound of the specified dimension in the Array. |
GetType | Gets the Type of the current instance. (Inherited from Object.) |
GetUpperBound | Gets the upper bound of the specified dimension in the Array. |
GetValue(Int32) | Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer. |
IndexOf(Array, Object) | Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array. |
Reverse(Array) | Reverses the sequence of the elements in the entire one-dimensional Array. |
SetValue(Object, Int32) | Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer. |
Sort(Array) | Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array. |
ToString | Returns a string that represents the current object. (Inherited from Object.) |