How do you use (get values ​​from keys, add items) Hashtables in F #

I would like to know how to use System.Collections.Hashtablein F #. The reason this is Hashtable is because I am referencing C # assemblies.

How can I name the following methods? - Add - Get the value from the key

I could not find anything useful on Google about this.

+5
source share
2 answers

As Mark points out, you can work with a type Hashtabledirectly from F # (as with any other .NET type). The syntax for accessing indexers in F # is slightly different, though:

open System.Collections 

// 'new' is optional, but I would use it here
let ht = new Hashtable()
// Adding element can be done using the C#-like syntax
ht.Add(1, "One")  
// To call the indexer, you would use similar syntax as in C#
// with the exception that there needst to be a '.' (dot)
let sObj = ht.[1] 

Hashtable , , , . :?> downcast, unbox , , :

let s = (sObj :?> string)
let (s:string) = unbox sObj

- , Dictionary<int, string> Hashtable. #, . F #, F # map IDictionary<_,_>, #:

let map = Map.empty |> Map.add 1 "one"
let res = map :> IDictionary<_, _>

, # , .

+11

.

open System.Collections //using System.Collections

let ht = Hashtable() // var ht = new Hashtable()

ht.Add(1, "One")

let getValue = ht.Item[1] // var getValue = ht[1];
//NB: All indexer properties are named "Item" in F#.
+2

All Articles