FirstOrDefault()

noun

say it: “FURST or dih-FAWLT” /fɜːrst ɔːr dɪˈfɔːlt/

The LINQ method that returns the first matching item, or a default such as null when there is none.

What does FirstOrDefault() do in C#?

FirstOrDefault() — A LINQ operation returning the first item, or the type's default value when none exists.

class NameFinder
{
    string? First(IEnumerable<string> names)
    {
        return names.FirstOrDefault();
    }
}

In a sentence

  • “FirstOrDefault(p => p.ProductId == 7) returned null when there was no product 7.”

Level: Freelancer · More C# terms at this level

Updated