Today I am discussing Arrays. Most of us are familiar with the concept of arrays as they work in almost all programming language. Arrays are a mutable collection type in F#. Here mutable means the values in an array can be changed after the array has been created. Arrays are ubiquitous; a familiar Data Structure used to represent a group of related, ordered values. An array contain a zero based, fixed size mutable sequence of consecutive data elements, all of the same type.
Arrays and Lists are both collections but an array has a number of properties which makes Arrays different from Lists. You can update the values which are in an Array but in a List you can not update. On the other hand a List can grow dynamically where as Arrays can not grow. Different Arrays called by different names like one dimensional arrays are referred as Vector and multidimensional are matrices.
You can define Arrays by Sequence of Items which can be separated by Semicolons. When you create an Array this will start by an opening square bracket and a Vertical bar([|) and end with a vertical bar and a Closing Square Bracket(|]).
Create And Access Array
Arrays can be created in one of the several ways. The general syntax for an array creation is as follows.
let ar=[| element1;element2;....... or array comprehension |] //syntax for creating Array
You can also put array items in separate lines; in that case the semicolon is optional.
// Syntax for creating Arrays in separate lines
let ar=
[|
element1
element2
.......
elementn
|]
Defining an Array literal
You can define an Array literal in the same way as lists by placing the array elements between this notation [||] like below.
//Defining an Array
let num=[|
"one";
"two";
"three";
"four"
|]
How to Reference an Array
The syntax for referencing an Array element is the name of the identifier of the array followed by a dot (.) known as a period and then the index of the element in Square brackets ([]) like below.
// syntax for referencing Array
let firstNum = num.[0]
let secondNum = num.[1]
let thirdNum = num.[2]
let fourthNum = num.[3]
How to set the value of an Array Element
You can set the value of an array element by left arrow and the value you want to assign followed by the left arrow.
//Syntax for setting Values to array elements
num.[0] <- " abc,"
num.[1] <- " def,"
num.[2] <- " ghi,"
num.[3] <- " jkl "
Example-





Join the conversation! Your thoughts help the community grow.