Created
August 4, 2022 15:07
-
-
Save rquackenbush/8aedc0e74acfa85863629eabd576f000 to your computer and use it in GitHub Desktop.
Simple extension method to pull a subtyped value from a dictionary.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
public static class IDictionaryExtensions | |
{ | |
public static bool TryGetValue<TKey, TOutValue, TInValue>(this IDictionary<TKey, TInValue> source, TKey key, out TOutValue value) | |
where TInValue : TOutValue | |
{ | |
if (source.TryGetValue(key, out var temp)) | |
{ | |
value= temp; | |
return true; | |
} | |
value = default; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment