i am working with LINQ there i use First and FirstOrDefault both return the same result what is the difference
Asked
Active
Viewed 4,117 times
2
-
have a look at to [this example](http://stackoverflow.com/documentation/c%23/68/linq-queries/329/first-firstordefault-last-lastordefault-single-and-singleordefault#t=201609220738336033875) – Adil Mammadov Sep 22 '16 at 07:38
2 Answers
4
First and Last throw an exception if no result was found. FirstOrDefault and LastOrDefault return the result variable type's default value in that case.

Patrick
- 668
- 4
- 11
0
FirstOrDefault and LastOrDefault would not throw an exception if there are no search item in sequence, they will return null.

Dmitriy Kovalenko
- 3,437
- 3
- 19
- 39
-
3Not exactly. They only return null if null is the return type's default value. In case of an integer, they return 0, etc. – Patrick Sep 22 '16 at 07:43
-
1Ah, if you u wont. They return **variable type`s default value**. – Dmitriy Kovalenko Sep 22 '16 at 07:49