Created
August 13, 2013 15:51
-
-
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.
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
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