Introduction: Collections allows us to
add and remove any number of elements dynamically. Generally we work with an array
when we have a fixed number of elements, because an array requires size (number) of
elements for allocating memory for them at the time of declaration. If elements
are less than the memory size of array then it is a waste of memory. But Collections
solve this problem. When we do not know how many elements or items are there,
then we use collection types. Collections types are found in
System.Collections.Generic namespace. Some of them are given below.
List<'T>: List is similar to array except - there is no need to specify the size at the time of declaration. Elements or items are added to or retrieved
from a List very fast. It dynamically adjusts memory size for elements. Now we
write the following code to understand List operations.
open
System.Collections.Generic
//Adding NameSpace
let lst =
new List<string>()//Declaring
List
lst.Add("A")
//Adding element to List
lst.Add("B")//Adding
element to List
lst.Add("C")//Adding
element to List
lst.Add("D")//Adding
element to List
lst.Add("E")//Adding
element to List
for elements
in lst do//Printing
items/elements of List
printfn "%s" elements
Output: 
Dictionary<'K ,'V>: It contains elements in the form of key - value
pairs. A Key is used to retrieved particular value in the List. Its structure is
like a hash-table. The following code demonstrates it working.
open
System.Collections.Generic
//Adding NameSpace
let dic =
new Dictionary<int, string>()//Declaring
Dictionary
dic.Add(1,"A")
//Adding element to Dictionary
dic.Add(2,"B")//Adding
element to Dictionary
dic.Add(3,"C")//Adding
element to Dictionary
dic.Add(4,"D")//Adding
element to Dictionary
dic.Add(5,"E")//Adding
element to Dictionary
for elements
in dic do//Printing
key/value of List
printfn "Key %d value %s" elements.Key
elements.Value



Join the conversation! Your thoughts help the community grow.