F# map to C# Dictionary

后端 未结 3 1532
臣服心动
臣服心动 2020-12-29 04:14

I\'m trying to convert an F# map to a C# dictionary, so far I am using:

    let toDictionary (map : Map<_, _>) : Dictionary<_, _> =
        let d         


        
相关标签:
3条回答
  • 2020-12-29 05:08

    A C# Dictionary will take an IDictionary in it's constructor. Map is an IDictionary so:

    let toDictionary (map : Map<_, _>) : Dictionary<_, _> = Dictionary(map)
    
    0 讨论(0)
  • 2020-12-29 05:15
    let toDictionary = Map.toSeq >> dict
    
    0 讨论(0)
  • 2020-12-29 05:17

    1. If you want the standard Dictionary implementation: See the answer of jbtule

    2. If you want an immutable, fast IDictionary: the dict function from the current F# core library allows to create a read-only dictionary from any sequence of key-value tuples:

    myDict |> Map.toSeq |> dict
    

    The return value has type IDictionary and should thus be usable from C#. Thanks to Daniel's answer for reminding me of Map.toSeq.

    3. If you're fine with using Map directly: Map<,> implements IDictionary, which should be convenient to use from C#. However, access of a map is O(log n), while hash-based dictionary access is constant order.

    0 讨论(0)
提交回复
热议问题