正如马克指出的,你可以和
Hashtable
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不是泛型的,所以您可能希望将对象转换回string。为此,您可以使用
:?>
downcast操作符,也可以使用
unbox
关键字并提供类型批注,以指定要作为结果获取的类型:
let s = (sObj :?> string)
let (s:string) = unbox sObj
如果您能控制所使用的类型,那么我建议您使用
Dictionary<int, string>
哈希表
map
把它向上推到
IDictionary<_,_>
在传给C之前:
let map = Map.empty |> Map.add 1 "one"
let res = map :> IDictionary<_, _>
这样,C#用户将看到一个熟悉的类型,但您可以用通常的函数样式编写代码。