MultiTool/MultiTool.Common/IDictionaryEx.vb
2022-04-06 14:55:15 +02:00

23 lines
704 B
VB.net

Imports System.Runtime.CompilerServices
Module IDictionaryEx
<Extension()>
Function GetOrDefault(Of TKey, TValue)(pDictionary As Dictionary(Of TKey, TValue), pKey As TKey, Optional pOnMissing As TValue = Nothing) As TValue
Dim oValue As TValue
If pKey Is Nothing Then
Return Nothing
End If
Return IIf(pDictionary.TryGetValue(pKey, oValue), oValue, pOnMissing)
End Function
<Extension()>
Function GetOrInsertNew(Of T, U As New)(dic As Dictionary(Of T, U), key As T) As U
If dic.ContainsKey(key) Then Return dic(key)
Dim newObj As U = New U()
dic(key) = newObj
Return newObj
End Function
End Module