linq.FirstOrDefault

Gets the first element in the array. Null is returned if the array contains zero elements.

Overloads

  • Object FirstOrDefault ()
  • Object FirstOrDefault (condition)

Object FirstOrDefault ()

Gets the first element in the array
var myArray = [1, 2, 3, 4];
//Returns 1
myArray.FirstOrDefault();

myArray = [];
//Returns null
myArray.FirstOrDefault();

Object FirstOrDefault (condition)

Gets the first element in the array where the specified condition is met

Example

var myArray = [1,3,4,5];
//Returns 4
myArray.FirstOrDefault("x => x % 2 == 0");

//Returns null
myArray.FirstOrDefault("x => x == 42");

Last edited Sep 24, 2011 at 7:41 AM by mlorbetske, version 2

Comments

No comments yet.