The .NET Framework contains different types of collections that can be used to store and retrieve data. In this post we will look at the different types.
Collections consist of:
- Queue
- Stack
- ArrayList
- List
- LinkedList
- Dictionary
Each collection type has different benefits.
Collections in the .NET Framework are in:
- System.Collections
- System.Collections.Generic
Queue
A queue is a FIFO (first in first out) list. You can think of this as like a line of customers. Dequeue removes the first item. Peek allows you to view the first item without dequeuing it.
Queue q = new Queue(); q.Enqueue("1"); q.Enqueue("2"); q.Enqueue("3"); q.Enqueue("4"); q.Enqueue("5"); Console.WriteLine("Dequeue - 1st item removed: {0}", q.Dequeue()); Console.WriteLine("Peek - look at 1st item in queue: {0}", q.Peek()); foreach (string o in q) { Console.WriteLine("Items now in queue: {0}", o); }
This produces:
Stack
A stack is a LIFO (last in first out) list. Push adds to the top of the list. So, if you add 1,2,3,4,5, the first item in the list [0] will be 5. Popping the stack removes the 1st element, in the example below, it removes 5. Peek allows you to view the 1st item, without popping:
Stack s = new Stack(); s.Push(1); s.Push(2); s.Push(3); s.Push(4); s.Push(5); Console.WriteLine(s.Pop()); Console.WriteLine(s.Peek()); foreach (int o in s) { Console.WriteLine(o); }
This produces:
ArrayList
ArrayLists can hold multiple types of data, and automatically expand as data is added.
Consider the code below. Note we have an object of a class Vehicle, defined as:
class Vehicle {}
We are creating an array list and adding to it strings, integers, and our object:
ArrayList AL = new ArrayList(); Vehicle v = new Vehicle(); AL.Add("1"); AL.Add(2); AL.Add(3); AL.Add(4); AL.Add(v); AL.Remove(2); foreach (object o in AL) { Console.WriteLine(o); } Console.ReadLine();
This produces:
Note, remove removes the first instance of what is provided, in this case 2.
Lists
Lists are strongly typed, i.e. the list is declared as a specific type at compile time. In the example below, we have a list of strings. If we tried to add an integer, it would not compile.
Lists allow for several operations, including contains, remove, insert, sort. You can access lists by index.
List<string> l = new List<string>(); l.Add("1"); l.Add("2"); l.Add("3"); l.Add("4"); l.Add("5"); l.Add("6"); if (l.Contains("4")) Console.WriteLine("Yes, list contains 4"); Console.WriteLine("What is in position 1? {0}", l[1]); Console.WriteLine("What is in position 5? {0}", l[5]); l.Remove("5"); l.Insert(0, "0"); l.Sort(); foreach (string o in l) { Console.WriteLine(o); }
This produces:
LinkedList
A linked list only allows sequential access. Note you can add before, after, first and last.
LinkedList l = new LinkedList(); l.AddLast(1); l.AddLast(2); l.AddLast(3); l.AddLast(4); l.AddLast(5); l.AddFirst(6); l.AddFirst(7); LinkedListNode n = l.Find(2); l.AddBefore(n, 5); foreach (int i in l) { Console.WriteLine(i); }
Dictionary
A dictionary is an array of key-value pairs.
Dictionary<string, string> l = new Dictionary<string, string>(); l.Add("1", "one"); l.Add("2", "two"); l.Add("3", "three"); l.Add("4", "four"); l.Add("5", "five"); l.Add("6", "six"); Console.WriteLine("Count", l.Count); List k = new List(l.Keys); foreach (string o in k) { Console.WriteLine("{0}", o); } List v = new List(l.Values); foreach (string o in v) { Console.WriteLine("{0}", o); } foreach (KeyValuePair<string, string> o in l) { Console.WriteLine("Key = {0}, Value = {1}", o.Key, o.Value); }
This produces:
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL