.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;

public class Program {
	public static void Main() {
		var enumerable = Enumerable.Range( 0, 100000 );
		Console.WriteLine( "Takelast" );
		takelast( enumerable );
		takelast( enumerable );
		takelast( enumerable );
		
		Console.WriteLine( "Takelast2" );
		takelast2( enumerable );
		takelast2( enumerable );
		takelast2( enumerable );
	}
	
	
	private static void takelast( IEnumerable<int> enumerable ) {
		var stop = Stopwatch.StartNew();
		var last3 = enumerable.TakeLast( 10 );
		Console.WriteLine( "Took: " + stop.ElapsedTicks );
		Console.WriteLine( "Last: " + string.Join( ", ", last3 ) );
	}
	
	private static void takelast2( IEnumerable<int> enumerable ) {
		var stop = Stopwatch.StartNew();
		var last3 = enumerable.TakeLast2( 10 );
		Console.WriteLine( "Took: " + stop.ElapsedTicks );
		Console.WriteLine( "Last: " + string.Join( ", ", last3 ) );
	}
}

public static class Extensions {
	/// <summary>
	/// Returns the last <paramref name="n"/> elements.
	/// </summary>
	public static IEnumerable<T> TakeLast<T>( this IEnumerable<T> c, int n ) {
		return c.Reverse().Take( n ).Reverse();
	}
	
	/// <summary>
	/// Returns the last <paramref name="n"/> elements.
	/// </summary>
	public static IEnumerable<T> TakeLast2<T>( this IEnumerable<T> c, int n ) {
		var count = c.Count();
		return c.Skip( count - n );
	}
}
;