Declaring


Fixed Size Arrays

In C# the number inside the bracket represents the number of items in the array

int[] myArraya = new int[3] { 1, 2, 3 }; 
int[] myArrayb = new int[] { 1, 2, 3 };
int[] myArrayc = { 1, 2, 3 };
var myArray = new[] {1, 2, 3}; // implicitly typed, inferred from items
System.Array myArrayd = System.Array.CreateInstance(typeof(System.Int32), 3);

When isize = 5, there are 5 elements, myArray(0) to myArray(4).

long[] myArray = new long[5]; 

int isize = 5;
long[] myArray = new long[isize];

long[] myArray = new long[3]; 
myArray[0] = 5;
myArray[1] = 10;
myArray.SetValue(15,2);


Dynamic Arrays

There are a number of ways you can declare a dynamic array.
In C# the square brackets must come after the type.

long[] myArray = new long[]; 

In C# it is good practice to include "new" when declaring and initialising an array on one line although you don't have to.

string[] myArray = new string[] {"one","two","three"} 
string[] myArray = string[] {"one","two","three"}
string[3] myArray = string[] {"one","two","three"} ??


Not Zero Based

//1 dimensional - zero based 
System.Array i2 = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 100 });

//1 dimensional - non zero based
System.Array myArray = Array.CreateInstance(typeof(double), new int[1] { 12 }, new int[1] { 1 });



© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext