Click here to Skip to main content
13,149,099 members (55,257 online)
Click here to Skip to main content

Stats

26.3K views
19 bookmarked
Posted 19 Jul 2008

Extension Methods Exemplified: Sorting Index-based Generic Lists

, 23 Aug 2008
This article shows how extension methods can be used, e.g., for sorting index-based generic lists.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Extensions;


namespace Sorting_Extensions
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a sequence of random integers.
            Random rand = new Random();
            var randomSequence = Enumerable
                .Repeat(0, 10)
                .Select(i => rand.Next(0, 10));

            IList<int> list = randomSequence.ToList<int>();
            PrintOnScreen(list);

            //Sort in using the extension method
            list.Sort();
            PrintOnScreen(list);

            Console.Write("\n\nPress any key to exit...");
            Console.ReadKey();
        }

        private static void PrintOnScreen<T>(IList<T> list)
        {
            foreach (T item in list)
                Console.Write("{0} ", item.ToString());
            Console.WriteLine();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Björn Friedrich
Other Leibniz Institute for Neurobiology Magdeburg
Germany Germany
- 2002 - 2004: vocational training (IT Specialist in Software Development) at the Deutsche Telekom AG with in-firm training at the T-Systems International GmbH (Berlin, Germany)

- 2003: three-month stay-abroad at the InterConsult Bulgaria Ltd. (Sofia, Bulgaria)

- 2004 - 2009: undergraduate studies in IT-Systems Engineering at the Hasso Plattner Institute for Software Systems Engineering (University of Potsdam, Germany)

- 2009: bachelor project at the Deutsche Post DHL (Berlin, Germany)

- 2009 - 2012: graduate studies in Integrative Neuroscience (Magdeburg, Germany)

- 2012 - present: research fellow at the Leibniz Institute for Neurobiology (Magdeburg, Germany)

- specialized in Microsoft .NET technologies
- currently develops scientific software with MatLab

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 23 Aug 2008
Article Copyright 2008 by Björn Friedrich
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid