Collections

Summary

The difference between lists and arrays are that lists have a dynamic size and arrays don't.

This tutorial explains the usages of the lists and their methods by using sample codes.

Coding

Let's assume we have the following code:

class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.Make = "Oldsmobile";
car1.Model = "Cutlas Supreme";

Car car2 = new Car();
car2.Make = "Geo";
car2.Model = "Prism";

Book b1 = new Book();
b1.Author = "Robert Tabor";
b1.Title = "Microsoft .NET XML Web Services";
b1.ISBN = "0-000-00000-0";



}
}

class Car
{
public string Make { get; set; }
public string Model { get; set; }
}

class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
}

ArrayList

Let's start with the ArrayList. The ArrayList contains a method for sorting and removing their values.

Initializing a new ArrayList looks the same as creating any other variable. The ArrayList includes a method "Add" and "Remove".

The ArrayList doesn't need an index because it uses the ID from the object.

The disadvantage of the ArrayList is that all data types can be stored which can lead to exceptions.

In our example it looks like this:

// ArrayLists haben eine dynamische Größe
// coole features wie sortieren, entfernen von elementen
ArrayList myArrayList = new ArrayList();
myArrayList.Add(car1);
myArrayList.Add(car2);
myArrayList.Add(b1);

foreach (Car car in myArrayList)
{
Console.WriteLine(car.Make);
}

This code throws an exception because it can't convert a book into a car.

So we remove it from the list by calling the "Remove" method.

// ArrayLists haben eine dynamische Größe
// coole features wie sortieren, entfernen von elementen
ArrayList myArrayList = new ArrayList();
myArrayList.Add(car1);
myArrayList.Add(car2);
myArrayList.Add(b1);
myArrayList.Remove(b1);

foreach (Car car in myArrayList)
{
Console.WriteLine(car.Make);
}

List<T>

The "T" stands for the data type. The list will only consist of values of this data type.

Like this, a list of mixed data types isn't possible.

The List<T> has the same methods as the ArrayList. The biggest difference between those two are the data types of the values.

Our example wouldn't look too different from the example with the ArrayList.

Keep in mind: that adding a book to a list of cars would throw an exception!

// List<T>
List<Car> myList = new List<Car>();
myList.Add(car1);
myList.Add(car2);
//myList.Add(b1);
foreach (Car car in myList)
{
Console.WriteLine(car.Model);
}

Dictionary<TKey, TValue>

The Dictionary<> is a list which saves the values with a unique key.

Would we have to search for a specific car in a List<> or ArrayList, we would have to run through the entire list.

In the initialization we declare which data type the key has and which data type the value has.

In our example we have to create a new field for the Car object because the key in the Dictionary<> has to be unique and our car doesn't have a unique field.

So, let's create a new property called VIN in our car-class and give our cars the VIN "A1" and "B2".

Our example, to look for a specific car, would look like this:

// Dictionary<TKey, TValue>
Dictionary<string, Car> myDicionary = new Dictionary<string, Car>();

myDicionary.Add(car1.VIN, car1);
myDicionary.Add(car2.VIN, car2);

Console.WriteLine(myDicionary["B2"].Make);

The access in the Dictionary<> is like the one in the array except by using an index we are using the key.

Because the returned value of the dictionary access is from the type "Car" we can use their variables.

Initializer

In the array-tutorial was already explained how to initialize a new array in one statement.

That looked similar to this:

string[] names = { "Bob", "Steve", "Brian", "Chuck" };

The same syntax can be used for creating new objects:

// Object initializer
// No need for a constructor
Car c1 = new Car() { Make = "BMW", Model = "750li", VIN = "C3" };
Car c2 = new Car() { Make = "Toyota", Model = "4runner", VIN = "D4" };

This makes it possible to create a new list of new initialized cars in one statement.

This has the advantage of writing less code.

// Collection initializer
List<Car> myList1 = new List<Car>() {
new Car { Make = "Oldsmobile", Model = "Cutlas Supreme", VIN = "E5"},
new Car { Make = "Nissan", Model = "Altima", VIN = "F6"}
};

Complete program code:

class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.Make = "Oldsmobile";
car1.Model = "Cutlas Supreme";
car1.VIN = "A1";

Car car2 = new Car();
car2.Make = "Geo";
car2.Model = "Prism";
car2.VIN = "B2";

Book b1 = new Book();
b1.Author = "Robert Tabor";
b1.Title = "Microsoft .NET XML Web Services";
b1.ISBN = "0-000-00000-0";


// ArrayLists haben eine dynamische Größe
// coole features wie sortieren, entfernen von elementen
ArrayList myArrayList = new ArrayList();
myArrayList.Add(car1);
myArrayList.Add(car2);
myArrayList.Add(b1);
myArrayList.Remove(b1);

foreach (Car car in myArrayList)
{
Console.WriteLine(car.Make);
}


// List<T>
List<Car> myList = new List<Car>();
myList.Add(car1);
myList.Add(car2);
//myList.Add(b1);
foreach (Car car in myList)
{
Console.WriteLine(car.Model);
}



// Dictionary<TKey, TValue>
Dictionary<string, Car> myDicionary = new Dictionary<string, Car>();

myDicionary.Add(car1.VIN, car1);
myDicionary.Add(car2.VIN, car2);

Console.WriteLine(myDicionary["B2"].Make);


string[] names = { "Bob", "Steve", "Brian", "Chuck" };

// Object initializer
// No need for a constructor
Car c1 = new Car() { Make = "BMW", Model = "750li", VIN = "C3" };
Car c2 = new Car() { Make = "Toyota", Model = "4runner", VIN = "D4" };


// Collection initializer
List<Car> myList1 = new List<Car>() {
new Car { Make = "Oldsmobile", Model = "Cutlas Supreme", VIN = "E5"},
new Car { Make = "Nissan", Model = "Altima", VIN = "F6"}
};


Console.ReadLine();
}
}

class Car
{
public string VIN { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}

class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
}