Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirstOrDefault Behavior with Int and Int?

Tags:

c#

linq

I just read a SO post that explained that FirstOrDefault()'s return type will vary based on the value of the element being selected.

Example:

ICollection<String> list = new List<String>();
list.Add("bye");

int a = (from x in list where (x == "hi") select x.Length).FirstOrDefault(); 

In this example, a will be equal to 0 since the int default value is 0.

However, I can append .Cast<int?>() as per the already linked post in order to get null when the query returns 0 results.

int? a = (from x in list where ... x.Length).Cast<int?>().FirstOrDefault();

Why don't I get a compile-time error (or at least a warning) when, for my first example, I use a Nullable int (int?) rather than a regular int?

If I understand correctly, using a int? when performing my first query will never result in a value of null.

like image 554
Kevin Meredith Avatar asked Jul 21 '13 17:07

Kevin Meredith


People also ask

What is the difference between first () and FirstOrDefault () Select methods in Linq?

The major 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.

What does FirstOrDefault mean?

FirstOrDefault<TSource>(IEnumerable<TSource>, TSource) Returns the first element of a sequence, or a specified default value if the sequence contains no elements. FirstOrDefault<TSource>(IEnumerable<TSource>) Returns the first element of a sequence, or a default value if the sequence contains no elements.

What is the difference between FirstOrDefault and SingleOrDefault?

SingleOrDefault() – Same as Single(), but it can handle the null value. First() - There is at least one result, an exception is thrown if no result is returned. FirstOrDefault() - Same as First(), but not thrown any exception or return null when there is no result.

What is FirstOrDefault method in C#?

Use the FirstorDefault() method to return the first element of a sequence or a default value if element isn't there. List<double> val = new List<double> { }; Now, we cannot display the first element, since it is an empty collection.


1 Answers

Why don't I get a compile-time error (or at least a warning) when, for my first example, I use a Nullable int rather than a regular int then? If I understand correctly, using a int? when performing my first query will never result in a value of null.

Your understanding is correct. However, the compiler does not interfere with your desire to declare variable a as nullable, because it is a "widening" conversion: even though the assignment from the LINQ query would never return a null, you may have other use for the same variable down below:

int? a = (from x in list where (x == "hi") select x.Length).FirstOrDefault();
// Do something with `a`, which will not be null
...
a = null;
if (someCondition) {
    a = someNotNullValue();
}
// Here, a may or may not be null
...
like image 85
Sergey Kalinichenko Avatar answered Oct 16 '22 17:10

Sergey Kalinichenko