Collections

introduction to collections in C# .NET

Advertisements

In last post I wrote about implementation of generics, and the difference between list and an array. Today we will take closer look at Collections. This is more general subject, mentioned earlier generics are part of System.Collection class.

Collections are used to group objects and simplify the process off adding, removing and manipulating data. Collections are a very good alternative of arrays. To see the difference we need to look at the arrays first. Let’s take an example, this array is storing object of simple class called Student.

Here is our Student Class

public class Student
 {
 public int ID { get; set; }
 public string Name { get; set; }
 public string Grade { get; set;}
 }

now we create few instances, like this one

 Student student1 = new Student()
 {
 ID = 123,
 Name = "John",
 Grade = "A"
 };

We will use those data for other examples as well.

Array of objects will look like this

Student[] arrayOfStudents = new Student[3];
 arrayOfStudents[0] = student1;
 arrayOfStudents[1] = student2;
 arrayOfStudents[2] = student3;

now let’s say that we want to ad another student to our class. can we just write

arrayOfStudents[3] = student4;

Answer is, No. Writing this we will not have any syntax errors, but our program will crash. Adding new items is just one example of how not flexible arrays are. Imagine you have to remove item and  than shift all indexes. Here’s  where collections really shine.

Array List

What is an ArrayList? In post about generics we analysed collection List<>. So what’s the difference between generic List<> and ArrayList?

ArrayList is part of System.Collections namespace, while List<> is inherent from System.Collections.Generics. ArrayList was part of earliest version of .NET, and it can store objects of different type like string and integer together.

ArrayList newlist = new ArrayList();
 newlist.Add(10);
 newlist.Add(36);
 newlist.Add("John");

This can cause many problems for example while we iterating through our array and we want to compare the  value with a number.

That’s why with Generic List<> we have declared type of the stored variables while we crating our list.

Basic Collections methods

Lets go back to previous example. This time lets use ArrayList insted

 ArrayList listOfStudents = new ArrayList();
 listOfStudents.Add(student1);
 listOfStudents.Add(student2);
 listOfStudents.Add(student3);

now we can simply add new students, by using Add method

 listOfStudents.Add(student4);

in similar way we can remove the object, by using Remove()

listOfStudents.Remove(student3);

Those are just simplest and most basic methods. List of full documentation you can find here .

One more interesting feature. You can convert Array into ArrayList, and reverse. To convert ListArray into array just use, in this example:

ArrayList newListOfStudents = new ArrayList(arrayOfStudents);
//Print data to make sure it works properly
 Console.WriteLine(newListOfStudents.IndexOf(student3));

In next part we will analyse other types of collections like Hash Tables, Binary Trees or Stacks. And we will see how to use properties of keys. Stay Awesome!

 

Author: indiedevart

Indie Games Developer

5 thoughts on “Collections”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s