Skip to content

Instantly share code, notes, and snippets.

@bradwilson
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradwilson/9200743 to your computer and use it in GitHub Desktop.
Save bradwilson/9200743 to your computer and use it in GitHub Desktop.
public struct Maybe<T>
{
public static readonly Maybe<T> NoValue;
public Maybe(T value, bool hasValue = true) : this()
{
HasValue = hasValue;
Value = value;
}
public bool HasValue { get; private set; }
public T Value { get; private set; }
}
public static class DictionaryExtensions
{
public static Maybe<TValue> MaybeGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
if (dictionary != null)
{
TValue value;
if (dictionary.TryGetValue(key, out value))
return new Maybe<TValue>(value);
}
return Maybe<TValue>.NoValue;
}
}
@bentayloruk
Copy link

A joy to have available in any solution! We have @jaredpar's Option in use over here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment