Skip to content

Instantly share code, notes, and snippets.

@gabrieljoelc
Created August 13, 2013 15:51
Show Gist options
  • Save gabrieljoelc/6222597 to your computer and use it in GitHub Desktop.
Save gabrieljoelc/6222597 to your computer and use it in GitHub Desktop.
Check if exactly none, exactly one, or many extension method from http://stackoverflow.com/a/6940337/34315 and http://stackoverflow.com/a/6059711/34315.
public static Multiplicity Multiplicity<TElement>(this IEnumerable<TElement> @this)
{
switch (@this.Take(2).Count())
{
case 0: return General.Multiplicity.None;
case 1: return General.Multiplicity.One;
case 2: return General.Multiplicity.Many;
default: throw new Exception("WTF‽");
}
}
public enum Multiplicity
{
None,
One,
Many,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment