First() and FirstOrDefault() are
not properties. Also whether it exists or not is irrelevent, and so is the question. I don't think you're understanding what I was trying to point out though.
Your code here is the only thing I was basing my statement off of. There is a reason why one is called First
OrDefault(), and the other is just called First().
You don't take into account the default return value for FirstOrDefault() and you use FirstOrDefault() as though to assume that there will always be something "found" by this query. If you're going to assume that you will always have at least one element to be able to return the First element of a query, without the LINQ query itself returning no elements, then you could just use First().
edit: Look...
- char var1 = "abcd".FirstOrDefault(c => c == 'x');
-
Console.WriteLine(var1 == null); // because x doesn't exist in that string...
-
char nvar2 = "abcd".First(c => c == 'x');
-
Console.WriteLine(var1 == null); // this part throws an exception because it expects that there will be a value...
Have you read the MSDN docs for FirstOrDefault() vs. First()?
There is not need to check whether the log is present or not.
If that is true, then there's no need for FirstOrDefault(), and you should instead probably just use First() if you're going to use LINQ here..
NOTE: *
Both First() and FirstOrDefault() are going to be slower than indexing though so why not just index 0 directly??
Example:
- Directory.GetFiles("Filepath")[0];