linq.LastOrDefault

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

Overloads

  • Object LastOrDefault ()
  • Object LastOrDefault (condition)

Object *LastOrDefault ()

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

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

Object LastOrDefault (condition)

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

Example

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

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

Last edited Sep 23, 2011 at 7:01 AM by mlorbetske, version 2

Comments

No comments yet.