Skip to content

Instantly share code, notes, and snippets.

@teglsbo
Forked from refactorsaurusrex/maybe.cs
Last active April 6, 2017 20:26
Show Gist options
  • Save teglsbo/6d4fa8a38154febd17933bd0cf477861 to your computer and use it in GitHub Desktop.
Save teglsbo/6d4fa8a38154febd17933bd0cf477861 to your computer and use it in GitHub Desktop.
Maybe<T>, inspired by Mark Seemann
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