Utilize FirstOrDefault
, if there is no return it returns the default value of the class that is null
(This varies according to type, if it is a int
for example the return is 0 as default value).
MaterialImage mainImage = images.FirstOrDefault(i => i.IsMainImage == true);
if (mainImage != null)
{
// teve retorno;
}
That one Default
of the method of Linnum is the same thing as default(T) and as has already been reported depending on the type it puts its default value.
For example:
using System;
public class Carros
{
public int Id {get;set;}
}
public class Test
{
public static void Main()
{
System.Console.WriteLine(default(int));
System.Console.WriteLine(default(long));
System.Console.WriteLine(default(DateTime));
System.Console.WriteLine(default(Carros) == null);
}
}
Exit:
0
0
1/1/0001 12:00:00 AM
True
Online Example
References:
What he’ll get if he goes Default?
– ihavenokia
@ihavenokia made the edit, if he does not find return
null
!– novic
Okay, so the difference is that with the Default he keeps null instead of giving Exception?
– ihavenokia
The
OrDefault
checks if there has been a return if it does not give a default(T) where T is the return type, which in your case is a class and every class the default value isnull
I’ll improve the answer.– novic
I’ve seen the changes, it’s 5* :)
– ihavenokia
@ihavenokia vlw
– novic