[C# Generics] Collection Classes

Since C# 2 introduced the generic collections, you have had no reason to use non-generic collections. I consider non-generic collections are deprecated and should not be used unless you are forced use only C# 1 features.    

 

1. Collection Interfaces

System.Collections           –       System.Collections.Generic

icollection

2. Collection Classes

System.Collections     –       System.Collections.Generic.

ArrayList                     –       List<T>
Hashtable                    –       Dictionary<TKey, TValue>
Queue                          –       Queue<T>
Stack                           –       Stack<T>
SortedList                   –       SortedList<TKey, TValue>

In most cases, you are using List<T>.

Dictionary<TK, TV>, Queue<T>, and Stack<T> can be used in some special cases. Never use non-generic collections if you’re using C# 2 or later (VS 2005 or later).

 

3. Collection Structs

System.Collections     –        System.Collections.Generic.

DictionaryEntry          –        KeyValuePair<TKey, TValue>

 

4. Sample Data Class

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

Point p1 = new Point { X = 10, Y = 10 };
Point p2 = new Point { X = 5, Y = 5 };

 

5.  ArrayList vs. List<T>

// ArrayList
var list1 = new ArrayList();
list1.Add(p1);
list1.Add(p2);

for (int i = 0; i < list1.Count; i++ )
{
    Point p = list1[i] as Point;
    if (p != null)
    {
        Console.WriteLine($"{p.X}, {p.Y}");
    }
}
// List<T>
var list2 = new List<Point>();
list2.Add(p1);
list2.Add(p2);

foreach (Point p in list2)
{
    Console.WriteLine($"{p.X}, {p.Y}");
}

 

6. HashTable vs. Dictionary<TKey, TValue>

// Hashtable
var table1 = new Hashtable();
table1.Add("P1", p1);
table1.Add("P2", p2);

foreach (DictionaryEntry t in table1)
{
    string key = t.Key as string;
    Point p = t.Value as Point;
    if (p != null)
    {
        Console.WriteLine($"{key} : {p.X}, {p.Y}");
    }
}

// Dictionary<T, K>
var table2 = new Dictionary<string, Point>();
table2.Add("P1", p1);
table2.Add("P2", p2);

foreach (KeyValuePair<string,Point> t in table2)
{
    Console.WriteLine($"{key} : {t.Value.X}, {t.Value.Y}");
}

 

7. Stack vs. Stack<T>

// Stack
var stack1 = new Stack();
stack1.Push(p1);
stack1.Push(p2);

int count = stack1.Count;
for (int i = 0; i < count; i++)
{
    Point p = stack1.Pop() as Point;
    if (p != null)
    {
        Console.WriteLine($"{p.X}, {p.Y}");
    }
}

// Stack<T>
var stack2 = new Stack<Point>();
stack2.Push(p1);
stack2.Push(p2);

count = stack2.Count;
for (int i = 0; i < count; i++)
{
    Point p = stack2.Pop();
    Console.WriteLine($"{p.X}, {p.Y}");
}

 

8. Queue vs. Queue<T>

// Queue
var queue1 = new Queue();
queue1.Enqueue(p1);
queue1.Enqueue(p2);

int count = queue1.Count;
for (int i = 0; i < count; i++)
{
    Point p = queue1.Dequeue() as Point;
    if (p != null)
    {
        Console.WriteLine($"{p.X}, {p.Y}");
    }
}

// Queue<T>
var queue2 = new Queue<Point>();
queue2.Enqueue(p1);
queue2.Enqueue(p2);

count = queue2.Count;
for (int i = 0; i < count; i++)
{
    Point p = queue2.Dequeue();
    Console.WriteLine($"{p.X}, {p.Y}");
}

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