Methods returning single item
Methods - Extract a single element
First() method
Intuitively enough, this extracts the first element in the sequence
Conditionally extract the first element
What happens if it was
val > 2.3
?
Single() method
Conditionally extract single element
What happens if it was
val > 2.1
?
FirstOrDefault() method
What do you think this call to FirstOrDefault
() would return?
SingleOrDefault() method
SingleOrDefault()
is a bit different from the others. If there is no element in the list that meets the criteria, then a default value is returned. If more than one value meets the criteria, then SingleOrDefault()
still throws an exception, just like the Single()
method.
With this in mind, what do you think this would do?
Can you try these for your own collection of other data types like string?
Last updated