Display a List in Console I was searching for a way to display a List&lt;T&gt; with n elements in a user friendly and easy to handle way within the console application. So I wrote this console-list-pagiation method.

Probably there are ways to improve the usability of my approach.

public static void ConsolePaging&lt;T&gt;(this IEnumerable&lt;T&gt; Values)
{
    int Pagesize = Console.WindowHeight - 2;
    int PageWidth = Console.WindowWidth - 1;
    int CurrentPage = 1;
    int PageCount = (Values.Count() + Pagesize - 1) / Pagesize;
    ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();
    do
    {
        if (keyInfo.Key == ConsoleKey.RightArrow || keyInfo.Key == (ConsoleKey)0 || keyInfo.Key == ConsoleKey.LeftArrow)
        {
            if (keyInfo.Key == ConsoleKey.RightArrow &amp;&amp; CurrentPage &lt; PageCount)
            {
                CurrentPage++;
            }
            else if (keyInfo.Key == ConsoleKey.LeftArrow &amp;&amp; CurrentPage &gt; 1)
            {
                CurrentPage--;
            }
            Console.Clear();
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkRed;
            string HeaderText = string.Format("Page:[{0}]/[{1}] │ Records:[{2}] ║ [◄] Previous │ [►] Next │ [Esc] Escape", CurrentPage, PageCount, Values.Count());
            Console.WriteLine(HeaderText.PadRight(Console.BufferWidth - 1, ' '));
            Console.ResetColor();
            foreach (T item in Values.Skip(Pagesize * (CurrentPage - 1)).Take(Pagesize))
            {
                string row = item.ToString();
                if (row.Length &gt; PageWidth)
                {
                    row = row.Substring(0, PageWidth);
                }
                Console.WriteLine(row);
            }
        }
        keyInfo = Console.ReadKey(true);
    } while (keyInfo.Key != ConsoleKey.Escape);
}


usage:

Enumerable.Range(1, 1000).ToList().ConsolePaging();
To my taste it's doing way to much. You'd be better off if you implemented the paging logic for a collection without mixing it with console interaction.

Write another method that expects a collection, page, page length etc. and do the navigation in another method.

If you do this you can test the paging alogorithm without the console and without key input. You'll be also able to test the navigation separately.

Even better, your paging method will be reusable.



To speed up the paging you could group the items by page. If you then ToList() it you can access each page directly without reiterating the collection each time:

public static IEnumerable&lt;IGrouping&lt;int, T&gt;&gt; GroupByPage&lt;T&gt;(this IEnumerable&lt;T&gt; values, int pageSize)
{
    var currentPage = 0;
    var page = new Func&lt;int, int&gt;(index =&gt; index &gt; 0 &amp;&amp; index % pageSize == 0 ? ++currentPage : currentPage);
    return values
        .Select((v, i) =&gt; new { v, i })
        .GroupBy(x =&gt; page(x.i), x =&gt; x.v)
        .ToList();
}




Here's another example with a decorator:

First we create an interface:

public interface IPager&lt;out T&gt;
{
    int PageIndex { get; }
    int PageSize { get; }
    int PageCount { get; }
    IGrouping&lt;int, T&gt; CurrentPage { get; }
    IGrouping&lt;int, T&gt; this[int page] { get; }
    void Next();
    void Prev();
}


Then we create a general pager:

public class Pager&lt;T&gt; : IPager&lt;T&gt;
{
    private int _pageIndex;

    private readonly IEnumerable&lt;IGrouping&lt;int, T&gt;&gt; _pages;

    public Pager(IEnumerable&lt;T&gt; values, int pageSize)
    {
        PageSize = pageSize;
        var currentPage = 0;
        var page = new Func&lt;int, int&gt;(index =&gt; index &gt; 0 &amp;&amp; index % pageSize == 0 ? ++currentPage : currentPage);
        _pages = values
            .Select((v, i) =&gt; new { v, i })
            .GroupBy(x =&gt; page(x.i), x =&gt; x.v);
    }

    public int PageIndex =&gt; _pageIndex;

    public int PageSize { get; }

    public int PageCount =&gt; _pages.Count();

    public IGrouping&lt;int, T&gt; this[int page] =&gt; _pages.ElementAtOrDefault(page);

    public IGrouping&lt;int, T&gt; CurrentPage =&gt; this[_pageIndex];

    public void Next() =&gt; _pageIndex++;

    public void Prev() =&gt; _pageIndex--;

}


Now we create another pager but this time it can display the item in the console:

public class ConsolePager&lt;T&gt; : IPager&lt;T&gt;
{
    private readonly IPager&lt;T&gt; _pager;

    public ConsolePager(IPager&lt;T&gt; pager)
    {
        _pager = pager;
    }

    public int PageIndex =&gt; _pager.PageIndex;

    public int PageSize =&gt; _pager.PageSize;

    public int PageCount =&gt; _pager.PageCount;

    public IGrouping&lt;int, T&gt; CurrentPage =&gt; _pager.CurrentPage;

    public void Next() =&gt; _pager.Next();

    public void Prev() =&gt; _pager.Prev();

    public IGrouping&lt;int, T&gt; this[int page] =&gt; _pager[page];

    public void Run()
    {
        var pageWidth = Console.WindowWidth - 1;
        var keyInfo = new ConsoleKeyInfo();

        do
        {
            if (keyInfo.Key == ConsoleKey.RightArrow || keyInfo.Key == (ConsoleKey)0 || keyInfo.Key == ConsoleKey.LeftArrow)
            {
                if (keyInfo.Key == ConsoleKey.RightArrow &amp;&amp; _pager.PageIndex &lt; _pager.PageCount - 1)
                {
                    _pager.Next();
                }
                else if (keyInfo.Key == ConsoleKey.LeftArrow &amp;&amp; _pager.PageIndex &gt; 1)
                {
                    _pager.Prev();
                }
                Console.Clear();
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                var headerText = string.Format("Page:[{0}]/[{1}] │ Records:[{2}] ║ [◄] Previous │ [►] Next │ [Esc] Escape", _pager.PageIndex + 1, _pager.PageCount, _pager.CurrentPage.Count());
                Console.WriteLine(headerText.PadRight(Console.BufferWidth - 1, ' '));
                Console.ResetColor();

                foreach (var item in _pager.CurrentPage)
                {
                    var row = item.ToString();
                    if (row.Length &gt; pageWidth)
                    {
                        row = row.Substring(0, pageWidth);
                    }
                    Console.WriteLine(row);
                }
            }
            keyInfo = Console.ReadKey(true);
        } while (keyInfo.Key != ConsoleKey.Escape);
    }
}


Usage:

new ConsolePager&lt;int&gt;(
    new Pager&lt;int&gt;(Enumerable.Range(1, 1000), 
    Console.WindowHeight - 2)
).Run();