C# FirstOrDefault (Get First Element If It Exists)

Use the FirstOrDefault method. FirstOrDefault gets the first element if it exists.

FirstOrDefault is almost the same as First. The difference is how it handles empty collections. If a collection is empty, it returns the default value for the type. The method internally checks if an element exists.First

Example. The System.Linq namespace is required to use FirstOrDefault. The FirstOrDefault method will appear in Visual Studio's IntelliSense feature by typing the period after an identifier that describes a type that implements IEnumerable.LINQ

Info: FirstOrDefault is invoked 4 times. The first time the method is invoked, it returns the value of the first string element in the List.

And: The second time it is called, it returns null because the "query1" expression returned no elements.

Next: The third usage returns the string literal "Mouse" because that is the first element that matches the "where" clause in the query.

Finally: The program displays 0 because that is the result of FirstOrDefault when called on an empty integer array.

C# program that uses FirstOrDefault using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // // This List has three elements. // var list = new List<string>() { "Cat", "Rat", "Mouse" }; Console.WriteLine(list.FirstOrDefault()); // // This query produces no results so FirstOrDefault is null. // var query1 = from element in list where element.Length > 10 select element; Console.WriteLine(query1.FirstOrDefault() == null); // // This query produces one result, so FirstOrDefault is a string. // var query2 = from element in list where element.Length > 3 select element; Console.WriteLine(query2.FirstOrDefault()); // // This array has no elements, so FirstOrDefault is zero. // int[] array = new int[0]; Console.WriteLine(array.FirstOrDefault()); } } Output Cat True Mouse 0

Internals. The FirstOrDefault method is a syntax extension found in a separate location in the base class library. It is a generic method which means it accepts a type parameter that indicates what types it acts upon.

Tip: You do not need to specify the type parameter because this is inferred during the C# compilation step.

Null: It checks the acting collection for null, and if that is true it returns the result of the default(T) expression.

And: Otherwise, it returns the element at index 0 of the element iterated to first.

Default expression. The C# language provides a default expression, which the FirstOrDefault method refers to. This expression is similar to the typeof operator. You can use default(int) or default(StringBuilder).

Info: The default value for a value type is equal to all zero bits. And the default value for a reference type is the null value.


Summary. This method is a shortcut to accessing the element that occurs first, while protecting against invalid accesses. FirstOrDefault is another way to access the "best match" from a query expression that sorts and filters elements.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to