Page 104 - CSharp/C#
P. 104
int[] arr = new int[] { 24, 2, 13, 47, 45 };
The new int[] portion can be omitted when declaring an array variable. This is not a self-contained
expression, so using it as part of a different call does not work (for that, use the version with new):
int[] arr = { 24, 2, 13, 47, 45 }; // OK
int[] arr1;
arr1 = { 24, 2, 13, 47, 45 }; // Won't compile
Implicitly typed arrays
Alternatively, in combination with the var keyword, the specific type may be omitted so that the
type of the array is inferred:
// same as int[]
var arr = new [] { 1, 2, 3 };
// same as string[]
var arr = new [] { "one", "two", "three" };
// same as double[]
var arr = new [] { 1.0, 2.0, 3.0 };
Iterate over an array
int[] arr = new int[] {1, 6, 3, 3, 9};
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
using foreach:
foreach (int element in arr)
{
Console.WriteLine(element);
}
using unsafe access with pointers https://msdn.microsoft.com/en-ca/library/y31yhkeb.aspx
unsafe
{
int length = arr.Length;
fixed (int* p = arr)
{
int* pInt = p;
while (length-- > 0)
{
Console.WriteLine(*pInt);
pInt++;// move pointer to next element
}
}
}
https://riptutorial.com/ 50

