firstOrDefault method

E firstOrDefault(
  1. E defaultValue
)

First element or defaultValue if the collection is empty.

var first = [1, 2, 3, 4].firstOrDefault(-1); // 1
var emptyFirst = [].firstOrDefault(-1); // -1

Implementation

E firstOrDefault(E defaultValue) => elementAtOrElse(0, (_) => defaultValue);