IDictionary<>.TryGet

namespace Foo

open System
open System.Collections.Generic

[<AutoOpen>]
module FsExtensions =

    type IDictionary<'Key,'Value> with
        /// Attempts to get the value associated with the specified key.
        member this.TryGet key =
            let ok, v = this.TryGetValue key
            if ok then Some v else None
Example:
match myDict.TryGet "foo" with
| Some(v) -> printfn "Found %A" v
| None    -> printfn "Not found!"

Description

Like IDictionary<>.TryGetValue, but returns the result as an Option, which is more convenient to work with from F#.

Details

Double click on the code to select all.

 

;