Created
April 17, 2018 14:37
-
-
Save SteGriff/67b2ef46bc4a6b1db8acc6d26f4b7789 to your computer and use it in GitHub Desktop.
How does LINQ Single behave?
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
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var myListOfNone = new List<Dog>(); | |
| var myListOfOne = new List<Dog>() { new Dog("Fido", 5) }; | |
| var myListOfSeveral = new List<Dog>() { new Dog("Fido", 5), new Dog("Lassy", 14) }; | |
| var b = myListOfNone.SingleOrDefault(); //Null | |
| var c = myListOfOne.Single(); //One | |
| var a = myListOfNone.Single(); //InvalidOperationException | |
| var d = myListOfSeveral.Single(); //InvalidOperationException | |
| var e = myListOfSeveral.SingleOrDefault(); //InvalidOperationException | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment