Difference Between First() And FirstOrDefault()?

Forums C#Difference Between First() And FirstOrDefault()?
Staff asked 2 years ago

Difference Between First() And FirstOrDefault()? can anyone explain me with the help of an example?

Answers (1)

Add Answer
Mustakim Khalifa Marked As Accepted
Staff answered 2 years ago

The difference between First and FirstOrDefault is that First() will throw an exception if there is no result data for the supplied criteria whereas FirstOrDefault() will return the default value (null) if there is no result data.

  • First() will throw an exception if there is result data is null, as you can see below.

First() Example

  •  FirstOrDefault() return null (default value). If, There is result data is null. As you can see below.

I hope this will help you to understand the difference between First() and FirstOrDefault().

Thank you.

Subscribe

Select Categories