Last active
August 29, 2015 13:56
-
-
Save bradwilson/9200743 to your computer and use it in GitHub Desktop.
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A joy to have available in any solution! We have @jaredpar's Option in use over here.