ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

IDictionary<>.TryGet

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

Source

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!"

Author: Joel Mueller

Submitted on: 29 okt 2010

Language: F#

Type: System.Collections.Generic.IDictionary

Views: 5115