Skip to content

Instantly share code, notes, and snippets.

@hymkor
Created February 23, 2018 10:19
Show Gist options
  • Save hymkor/df48a9a4f94b1bdfe88f2351a06a3000 to your computer and use it in GitHub Desktop.
Save hymkor/df48a9a4f94b1bdfe88f2351a06a3000 to your computer and use it in GitHub Desktop.
If Not dic1.TryGetValue(key1,val1) Then val1 = New Foo が多すぎて困る
Imports System.Runtime.CompilerServices
Module DictExtension
<Extension()>
Public Function GetOr(Of K, V)(ByVal dict As IDictionary(Of K, V), key As K, ByVal f As Func(Of V)) As V
Dim val As V = Nothing
If Not dict.TryGetValue(key, val) Then
val = f()
dict.Add(key, val)
End If
Return val
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment