Differences between Single and First in C#
In this tutorial we will take a closer look at few methods used to retrieve items from collections:
Single()
SingleOrDefault()
First()
FirstOrDefault()
Firstly, we will prepare our list:
List<Student> students = new List<Student>();
students.Add(n...
kdebowski.pl2 min read
kndb
hustling in the microsoft stack
Thanks for the article Kamil, good refresher.
I hate null handling, so recently I have been using these methods a bit differently, where I specify what is the default value.
var defaultStudent = new Student { Id = 0, Name = "Karen", Course = "FlatEarth" }; var student = students.Where(s => s.Name == "Charlie").DefaultIdEmpty(defaultStudent).FirstOrDefault();Hopefully MS would allow us to manage default values directly in the future.