public struct Maybe where T : class { T Value; public static Maybe Just(T t) { return new Maybe() { Value = t }; } public static Maybe Nothing { get { return new Maybe(); } } public static implicit operator Maybe(T value) { return new Maybe() { Value = value }; } public void Guard(Action valuePresent, Action valueAbsent) { if (Value != null) { if (valuePresent != null) valuePresent(Value); } else if (valueAbsent != null) valueAbsent(); } public TResult Guard(Func valuePresent, Func valueAbsent) { if (Value != null) { if (valuePresent != null) return valuePresent(Value); } else if (valueAbsent != null) return valueAbsent(); throw new ArgumentException("A Maybe guard case was missing and hit"); } }