.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

Collection Classes in .net - the LinkedList

This is the eighth part of an article on collection classes in .net with C#.  This part covers the LinkedList.

For the first part see http://www.audacs.co.uk/ViewPage.aspx?PageID=512

For the next part on the StringCollection see http://www.audacs.co.uk/ViewPage.aspx?PageID=521

I remember coding Linked Lists in C many many years ago which was lots of fun.  The .net framework provides a LinkedList class out of the box in the form of:

System.Collections.Generic.LinkedList<T>

So this is a collection of objects as specified by the T.  Each node in the collection is actually a LinkedListNode.

The node item then has a property called Value which is a reference to the actual object.

Each node in the list contains a reference to its preceeding node and the next node.  For the first node then the preceeding node reference will be null of course and likewise the last node will have a null reference for its next node.  Many .net collections are probably linked lists under the hood I would bet but this one exposes typical linked list methods to us.  So its reason for existing is for us to use when we are modelling a problem which behaves like a linked list.  This could be something like a GPS route finder application where we have a list of nodes representing points on a map or route.

Lets dive in and have a look at some code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace LinkedList
{
    public class Program
    {
        static void Main(string[] args)
        {
            System.Collections.Generic.LinkedList<MapLocation> map = new LinkedList<MapLocation>();
 
            MapLocation Rugeley = new MapLocation("Rugeley", 52.7570377, -1.9346097);
            MapLocation Cannock = new MapLocation("Cannock", 52.6881217, -2.0290249);
            MapLocation Wolverhampton = new MapLocation("Wolverhampton", 52.5857278, -2.1292507);
            MapLocation Oxford = new MapLocation("Oxford", 51.7522764, -1.2558243);
            MapLocation London = new MapLocation("London", 51.5001524, -0.1262362);
 
            map.AddFirst(Rugeley);
 
            map.AddAfter(map.Find(Rugeley), Cannock);
            map.AddAfter(map.Find(Cannock), Wolverhampton);
            map.AddAfter(map.Find(Wolverhampton), Oxford);
            map.AddAfter(map.Find(Oxford), London);
 
            foreach (MapLocation location in map)
            {
                Console.WriteLine(location);
            }
 
            Console.WriteLine();
 
            Console.WriteLine("Oxford: " + map.Find(Oxford).Value);
            Console.WriteLine("Previous to Cannock: " + map.Find(Cannock).Previous.Value);
            Console.WriteLine("Next from Cannock: " + map.Find(Cannock).Next.Value);
            Console.WriteLine("First item: " + map.First.Value);
            Console.WriteLine("Last item: " + map.Last.Value);
 
            Console.Read();
        }
    }
 
    public class MapLocation
    {
        public string PlaceName { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
 
        public MapLocation(string name, double latitude, double longitude)
        {
            PlaceName = name;
            Latitude = latitude;
            Longitude = longitude;
        }
 
        public override string ToString()
        {
            return PlaceName + " (Lat: " + Latitude.ToString() + " Long: " + Longitude.ToString() + ")";
        }
    }
}
 


Ok so first we create an instance of a LinkedList and we call this map.

We then create 5 instances of a MapLocation class which is found at the bottom of the code listing.

We then add the Rugeley object to the map using the AddFirstMethod.

Next we use the AddAfter method to add the rest of the locations.  This method takes 2 arguments.  The first is a LinkedListNode<T> instance and the second is the MapLocation object to add.  The LinkedListNode<T> instance in this case is returned from the Find method and it is this that we are adding after.

We then use a foreach loop to iterate over the List and output the contents to screen.

We then demonstrate a variety of methods and properties such as First, Last, Previous etc.  All of these return a LinkedListNode<T> object and that object has a Value property which is a reference to the actual object - in this case the MapLocation.  So basically each MapLocation object is wrapped in a LinkedListNode object.

There is of course much more you can do with a LinkedList but this is enough to give you the basic idea and allow you to write some code and do some experimenting.

For the next part on the StringCollection see http://www.audacs.co.uk/ViewPage.aspx?PageID=521