Use the var keyword to create an array without specifying its type explicitly:
xxxxxxxxxx
// create an array of integers
var numbers = new int[] {1, 2, 3, 4, 5};
This will create an array of integers with five elements and assign it to the numbers variable.
xxxxxxxxxx
// Define and Initialize
int[] arr = {1, 2, 3};
// Buuuut:
// Initial defining
int[] arr;
// This works
arr = new int[] {1, 2, 3};
// This will cause an error
// arr = {1, 2, 3};
xxxxxxxxxx
int[] Nameofarray = new int[how much is the max];
for example:
int[] X = new int[5];
xxxxxxxxxx
//Creating an empty array
string[] myString;
//Creating an occupied array
int[] myNumbers = {3,7,9,12};
xxxxxxxxxx
// Syntax:
datatype_variable[dimension] = new datatype[size]{array};
// For example:
string MyArray[] = new string[1]{"Hello","World"};
// or just:
string MyArray[]={"Hello","world"};
// for multidimensions:
// 2D array:
// 2 arrays, 3 values //
int MyArray=[,]=new int[1,2]{
{1,2,3},
{1,2,3}
}
// 3D array:
// 2 arrays, 3 arrays, 4 values //
int MyArray=[,,]=new int[1,2,3]{
{
{1,2,3,4},
{1,2,3,4},
{1,2,3,4}
},
{
{1,2,3,4},
{1,2,3,4},
{1,2,3,4}
}
}
xxxxxxxxxx
// Syntax to create a new array
dataType[] arrayName = new dataType[arrayLength];
The first example creates an array of integers with five elements, while the second example creates an array of strings with three initial elements. The third example creates a two-dimensional array with two rows and two columns.
xxxxxxxxxx
// create an array of integers
int[] numbers = new int[5];
// create an array of strings with initial values
string[] names = new string[] {"John", "Jane", "Bob"};
// create a multidimensional array
int[,] matrix = new int[2,2];