# Are there any ‘underlying’ differences between Map.TryFind , Map.tryfind and Map.TryGetValue?

**URL:** https://forums.fsharp.org/t/are-there-any-underlying-differences-between-map-tryfind-map-tryfind-and-map-trygetvalue/2988
**Category:** General
**Tags:** beginners
**Created:** [March 22, 2023, 1:56pm UTC](https://forums.fsharp.org/t/are-there-any-underlying-differences-between-map-tryfind-map-tryfind-and-map-trygetvalue/2988 "2023-03-22T13:56:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![GarryP](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GarryP](https://forums.fsharp.org/u/GarryP)
#### Post date: [March 22, 2023, 1:56pm UTC](https://forums.fsharp.org/t/are-there-any-underlying-differences-between-map-tryfind-map-tryfind-and-map-trygetvalue/2988/1 "2023-03-22T13:56:08Z")

</div>

Given this code:

```auto
let myMap = Map [(1,"one") ; (2, "two") ; (3, "three")]
let one = myMap.TryFind 1
let two = myMap |> Map.tryFind 2
let three = myMap.TryGetValue 3

```

…I get the following output:

```auto
val one: string option = Some "one"
val two: string option = Some "two"
val three: bool * string = (true, "three")

```

…and that’s all fine.

However, are there any advantages/disadvantages performance/style/other-wise in using one over the others?

Are any of them considered to be ‘old’ and/or ‘frowned upon’ in any way?

Is one of them much ‘better’ than the others for some non-obvious reason?

Should I normally be using one, and under which circumstances would the advice be different?

---

<div class="post-metadata">

### Author: ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)
#### Post date: [March 22, 2023, 5:24pm UTC](https://forums.fsharp.org/t/are-there-any-underlying-differences-between-map-tryfind-map-tryfind-and-map-trygetvalue/2988/2 "2023-03-22T17:24:56Z")

</div>

I would recommend prefering one of the `tryFind` variants over `TryGetValue`. `TryGetValue` comes from the `IDictionary` interface that `Map` implements, so it’s the more C# approach. Working with an `option` type is more idiomatic and safer than getting a bool + a string which could be null whenever the bool is false.

Between the two `tryFind` variants, it’s largely up to preference, and to some extent, context. The `Map.tryFind` function is going to work better with function composition (`>>`), with pipelines (`|>`), and with the type inference system without needing an annotation (like `let fn xs = Map.tryFind "key" xs` compiles but `let fn xs = xs.TryFind "key"` doesn’t unless you add an annotation).  
The `myMap.TryFind` method is going to work better with “fluent” C# APIs (like `myMap.TryFind("key").AddService(myService).Build()`) and is less total code.

---

<div class="post-metadata">

### Author: ![GarryP](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GarryP](https://forums.fsharp.org/u/GarryP)
#### Post date: [March 23, 2023, 1:48pm UTC](https://forums.fsharp.org/t/are-there-any-underlying-differences-between-map-tryfind-map-tryfind-and-map-trygetvalue/2988/3 "2023-03-23T13:48:47Z")

</div>

Thanks for that.  
I’d come to some similar conclusions but thought I’d ask in case there were details which I hadn’t taken into account.  
Cheers.
