Skip to content

Instantly share code, notes, and snippets.

@rquackenbush
Created August 4, 2022 15:07
Show Gist options
  • Save rquackenbush/8aedc0e74acfa85863629eabd576f000 to your computer and use it in GitHub Desktop.
Save rquackenbush/8aedc0e74acfa85863629eabd576f000 to your computer and use it in GitHub Desktop.
Simple extension method to pull a subtyped value from a dictionary.
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