First (find)

Download .net eBook

Example

var numbers = new[] {1,2,3,4,5};

var firstNumber = numbers.First();
Console.WriteLine(firstNumber); //1

var firstEvenNumber = numbers.First(n => (n & 1) == 0);
Console.WriteLine(firstEvenNumber); //2

The following throws InvalidOperationException with message "Sequence contains no matching element":

var firstNegativeNumber = numbers.First(n => n < 0);

Stats

Contributors: 3
2016-07-21
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook