Skip to content

Instantly share code, notes, and snippets.

@bradwilson bradwilson/Maybe.cs
Last active Aug 29, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Feb 25, 2014

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
You can’t perform that action at this time.