ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Penultimate

Gets the penultimate item of a collection

Source

public static T Penultimate<T>(this IEnumerable<T> source) => source.Reverse().Skip(1).Take(1).First();

Example

var list = new List<Int32> { 1, 2, 3 };
var penultimate = list.Penultimate();
//penultimate = 2

Author: Lu.Ca

Submitted on: 19 aug. 2020

Language: csharp

Type: ienumerable-t

Views: 3767