Difference between firstordefault and singleordefault

Both are the element operators and they are used to select a single element from a sequence. But there is a minor difference between them. SingleOrDefault() operator would throw an exception if more than one elements are satisfied the condition where as FirstOrDefault() will not throw any exception for the same

Single() – There is exactly 1 result, if no result is returned or more than one result then exception is thrown .

SingleOrDefault() – Same as Single(), but it can handle the null value.

First() – There is at least one result,  if no result is returned then  exception is thrown.

FirstOrDefault() – Same as First(), but not thrown any exception or return null when there is no result.


Leave a Reply

Your email address will not be published. Required fields are marked *