Author - Dave Amour

Dave Amour has used computers for as long as he can remember and intially started out as an IT trainer delivering a range of IT courses but for the last 11 years has been focusing on the arena of web application development. He was worked for numerous companies over the years and is currently working for Audacs Software Ltd. Dave is also a keen squash player and an active and sucessful member of Experts Exchange

Please feel free to submit any constructive comments which you can do at the bottom of this page.

Dave may be available for programming tuition or consultancy work. Contact via dave@audacs.co.uk
Dave Amour - Click to view CV 

Collection Classes in .net - the Generic List

This is the third part of an article on collection classes in .net with C#.  This part covers the Generic List.

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

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

Ok so what is a Generic List?

Well it is very simillar to an ArrayList but it uses generics to make the types well known and safe at compile time.

Before generics we used to have may classes which would use objects in order to allow them to be flexible.  The ArrayList for example is a collection of objects and so you can store anything in there - a collection of string, ints, customers, database connections - absolutley anything since all classes ultimatley inherit from System.Object.

This is good but it does have a few downsides.  First of all when retrieving items out of an ArrayList we have to then cast them to their specific class in order to use them.  This leads to extra code and extra resources.  Also though it can lead to runtime errors if one of the objects in the collection isn't the expected type when a cast is performed.

To solve these problems Microsoft introduced generics.  A generic list is the generic version of an ArrayList.  We might use one as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace GenericList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> people = new List<Person>();
 
            people.Add(new Person("Dave", 42));
            people.Add(new Person("Fred", 31));
            people.Add(new Person("Andrea", 26));
            people.Add(new Person("Sarah", 29));
 
            foreach (Person p in people)
            {
                Console.WriteLine(p);
            }
 
            Console.Read();
        }
    }
    public class Person
    {
        //Constructor
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
 
        //The persons name
        public string Name
        {
            get;
 
            set;
        }
 
        //The persons age
        public int Age
        {
            get;
 
            set;
        }
 
        public override string ToString()
        {
            return Name + " (age " + Age.ToString() + ")";
        }
    }
}
 


So you can see that it is used very much like an old ArrayList other than when we declare it me put the type which the collection will hold within the angle brackets.

We then get compile time type checking.  If we have code which tries to add some other type to the list then we will get a compile time error rather than a runtime one - somehting which is clearly much more preferable.

There are many many other places where generics are used of course but that is beyond the scope of theis article.

So if you are familliar with an ArrayList then you should be comfortable with a Generic List and theres not really much else to say really.

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

Leave a comment

Name

Email (optional and not disclosed)



Email address is not disclosed but just used to alert you of new posts if entered
Word CV
HTML CV
PDF CV
Text CV
CMS Lite
ETraining
Planet Health
Florida Health
the Date Shack
Taylors
Emcat
Emtex
Browne Jacobson
Alliance & Leicester
Baird Leisure
Swarfega
Creature Comforts
Rugeley Chess Club
Katnip
Katmaid
Dudley NHS
Contact Me
Current Status