I would like to know how to use a System.Collections.Hashtable in F#. The reason it is a Hashtable is because I am referencing C# assemblies.
How would I call the following methods?
– Add
– Get value from key
I have not been able to find anything useful in Google about this.
As Mark points out, you can work with the
Hashtabletype directly from F# (just like with any other .NET type). The syntax for accessing indexers in F# is slightly different though:Since Hashtable is not generic, you would probably want to cast the object back to string. To do that, you can either use the
:?>downcast operator, or you can use theunboxkeyword and provide a type annotation to specify what type do you want to get as the result:If you have any control over what type is used, then I would recommend using
Dictionary<int, string>instead ofHashtable. This is fully compatible with C# and you would avoid the need to do casting. If you’re returning this as a result from F#, you could also use standard F#mapand just upcast it toIDictionary<_,_>before passing it to C#:This way, C# users will see a familiar type, but you can write the code in the usual functional style.