the dictionary<'tkey, 'tvalue> class is the mutable analog of the f# map data structure and contains many of the same functions.
recapitulating from the map chapter in f#, a map is a special kind of set that associates the values with key.
creating of a mutable dictionary
mutable dictionaries are created using the new keyword and calling the list's constructor. the following example demonstrates this −
open system.collections.generic
let dict = new dictionary<string, string>()
dict.add("1501", "zara ali")
dict.add("1502","rishita gupta")
dict.add("1503","robin sahoo")
dict.add("1504","gillian megan")
printfn "dictionary - students: %a" dict
when you compile and execute the program, it yields the following output −
dictionary - students: seq [[1501, zara ali]; [1502, rishita gupta]; [1503, robin sahoo]; [1504, gillian megan]]
the dictionary(tkey,tvalue) class
the dictionary(tkey, tvalue) class represents a collection of keys and values.
the following tables provide the properties, constructors and the methods of the list(t) class −
properties
| property | description |
|---|---|
| comparer | gets the iequalitycomparer(t) that is used to determine equality of keys for the dictionary. |
| count | gets the number of key/value pairs contained in the dictionary(tkey, tvalue). |
| item | gets or sets the value associated with the specified key. |
| keys | gets a collection containing the keys in the dictionary(tkey, tvalue). |
| values | gets a collection containing the values in the dictionary(tkey, tvalue). |
constructors
| constructors | description |
|---|---|
| dictionary(tkey, tvalue)() | initializes a new instance of the dictionary(tkey, tvalue) class that is empty, has the default initial capacity, and uses the default equality comparer for the key type. |
| dictionary(tkey, tvalue)(idictionary(tkey, tvalue)) | initializes a new instance of the dictionary(tkey, tvalue) class that contains elements copied from the specified idictionary(tkey, tvalue) and uses the default equality comparer for the key type. |
| dictionary(tkey, tvalue)(iequalitycomparer(tkey)) | initializes a new instance of the dictionary(tkey, tvalue) class that is empty, has the default initial capacity, and uses the specified iequalitycomparer(t). |
| dictionary(tkey, tvalue)(int32) | initializes a new instance of the dictionary(tkey, tvalue) class that is empty, has the specified initial capacity, and uses the default equality comparer for the key type. |
| dictionary(tkey, tvalue)(idictionary(tkey, tvalue), iequalitycomparer(tkey)) | initializes a new instance of the dictionary(tkey, tvalue) class that contains elements copied from the specified idictionary(tkey, tvalue) and uses the specified iequalitycomparer(t). |
| dictionary(tkey, tvalue)(int32, iequalitycomparer(tkey)) | initializes a new instance of the dictionary(tkey, tvalue) class that is empty, has the specified initial capacity, and uses the specified iequalitycomparer(t). |
| dictionary(tkey, tvalue)(serializationinfo, streamingcontext) | initializes a new instance of the ictionary(tkey, tvalue) class with serialized data. |
methods
| method | description |
|---|---|
| add | adds the specified key and value to the dictionary. |
| clear | removes all keys and values from the dictionary(tkey, tvalue). |
| containskey | determines whether the dictionary(tkey, tvalue) contains the specified key. |
| containsvalue | determines whether the dictionary(tkey, tvalue) contains a specific value. |
| equals(object) | determines whether the specified object is equal to the current object. (inherited from object.) |
| finalize | allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (inherited from object.) |
| getenumerator | returns an enumerator that iterates through the dictionary(tkey, tvalue). |
| gethashcode | serves as the default hash function. (inherited from object.) |
| getobjectdata | implements the system.runtime.serialization.iserializable interface and returns the data needed to serialize the dictionary(tkey, tvalue)instance. |
| gettype | gets the type of the current instance. (inherited from object.) |
| memberwiseclone | creates a shallow copy of the current object. (inherited from object.) |
| ondeserialization | implements the system.runtime.serialization.iserializable interface and raises the deserialization event when the deserialization is complete. |
| remove | removes the value with the specified key from the dictionary(tkey, tvalue). |
| tostring | returns a string that represents the current object. (inherited from object.) |
| trygetvalue | gets the value associated with the specified key. |
example
open system.collections.generic
let dict = new dictionary<string, string>()
dict.add("1501", "zara ali")
dict.add("1502","rishita gupta")
dict.add("1503","robin sahoo")
dict.add("1504","gillian megan")
printfn "dictionary - students: %a" dict
printfn "total number of students: %d" dict.count
printfn "the keys: %a" dict.keys
printf"the values: %a" dict.values
when you compile and execute the program, it yields the following output −
dictionary - students: seq [[1501, zara ali]; [1502, rishita gupta]; [1503, robin sahoo]; [1504, gillian megan]] total number of students: 4 the keys: seq ["1501"; "1502"; "1503"; "1504"] the values: seq ["zara ali"; "rishita gupta"; "robin sahoo"; "gillian megan"]