Arrays in C#

An array is used to assign a series of elements of the same type in consecutive memory locations; to define a list of elements.

dataType[] arrayName = new dataType[arraySize]; 
dataType[] arrayName = {values,..,…};
dataType[,] arrayName = new dataType[rows, columns]; 
dataType[][] arrayName = new dataType[rows][columns];

arrayName[elementPosition] = arrayElement; 
//To set a value at a specific position in the array 

variableType Variable = arrayName[elementPosition]; 
//To access a specific element and store in a variable. 


dataType can be any C# data type, all elements in the array must be of this data type. arrayName is defined by the programmer. arraySize and elementPosition are integers.

The index value (elementPositions) starts at 0.

int[] array = {1,2,3}; 
array[4] = 4; //Note: new array is = {1,2,3,0,4} 
int startValue = array[0];

Add to Slack

< Pointers and References   |   Lists >

© 2018 SyntaxDB. All Rights Reserved.