Generic Collections in C#: List, Dictionary, Stack and Queue

The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

Dictionary<TKey, TValue> Represents a collection of keys and values.

List<T> Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

Queue<T> Represents a first-in, first-out (FIFO) collection of objects

Stack<T> Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.

Generic List Example:

Create a new ASP.NET project. Add a new item type “Class”, name it “TrainingPerson.cs”

public class TrainingPerson
   public string Name { get; set;}
   public string Age { get; set;}

Add new WebForm TestGenericCollection.aspx.cs

Add a button in the designer.

using System.Collections.Generic
protected void Button1_Click(object sender, EvenArgs e)
  List<TrainingPerson> employees = new List<TrainingPerson>();
  TrainingPerson person = new TrainingPerson();
  person.Name = "Michael Douglas";
  person.Age = 68;

  TrainingPerson person2 = new TrainingPerson();
  person2.Name = "Sylvester Stallone";
  person2.Age = 24;


  // Iterate through the List Collection
  foreach (TrainingPerson item in employees)
       Response.Write(item.Name + " is " + item.Age + " old." + "<BR>");


Generic Dictionary Example:

Add a class name it Customers.cs

   class Customer
      public int SosSecNo { get; set; }
      public string Name { get; set; }
      public Customer(int sossecno, string name)
         SosSecNo = sossecno;
         Name = name;

Use the same WebForm TestGenericCollection.aspx.cs, comment the previous “List” code.

// Define a Dictionary
Dictionary<int, Customer> mycustomers = new Dictionary<int, Customer>();

// Create instances of customer class
Customer cust1 = new Customer(190, "Michael");
Customer cust2 = new Customer(267, "Romeo");
Customer cust3 = new Customer(345, "Bill");

// Add customer class to Dictionary
mycustomers.Add(cust1.SosSecNo , cust1);
mycustomers.Add(cust2.SosSecNo, cust2);
mycustomers.Add(cust3.SosSecNo, cust3);

// Iterate through the Dictionary Collection
foreach (KeyValuePair<int, Customer> custObject in mycustomers)
   Response.Write(custObject.Key + " " + custObject.Value.Name + "<BR>");


Generic Stack Example:

Use the same WebForm TestGenericCollection.aspx.cs, comment the previous “List” code.

// Define a Stack
Stack<string> stacknumbers = new Stack<string>();

// add items to the Stack

// remove item from the Stack (last element)

// Iterate through the Stack Collection
foreach (string number in stacknumbers)
   Response.Write(number + "<BR>");


Generic Queue Example:

Use the same WebForm TestGenericCollection.aspx.cs, comment the previous “List” code.

Queue<string> Queuenumbers = new Queue<string>();

// Add elements to the queue
Queuenumbers.Enqueue("First item in the q")
Queuenumbers.Enqueue("Second item in the q")
Queuenumbers.Enqueue("Third item in the q")
Queuenumbers.Enqueue("Fourth item in the q")

// Remove elements from the queue (first element)

foreach (string number in Queuenumbers)
Response.Write(number + "<BR>");


This entry was posted in CSharp, Generics, Visual Studio 2012 and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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