-
-
Save teglsbo/6d4fa8a38154febd17933bd0cf477861 to your computer and use it in GitHub Desktop.
Maybe<T>, inspired by Mark Seemann
This file contains hidden or 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
using System.Collections; | |
using System.Collections.Generic; | |
namespace Dev_Maybe | |
{ | |
public class Maybe<T> : IEnumerable<T> | |
{ | |
readonly IEnumerable<T> values; | |
public Maybe() | |
{ | |
values = new T[0]; | |
} | |
public Maybe(T value) | |
{ | |
if (value == null) | |
values = new T[0]; | |
else | |
values = new[] { value }; | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return values.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} | |
public static class Maybe | |
{ | |
public static Maybe<T> Create<T>() | |
{ | |
return new Maybe<T>(); | |
} | |
public static Maybe<T> Create<T>(T value) | |
{ | |
return new Maybe<T>(value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment