Collections and Generics in .NET Framework

Overview of Collections in .NET Framework

  • Definition of Collections: A group of objects in the .NET Framework that provides a standard set of types for storing and managing collections of objects, including lists, linked lists, dictionaries, and arrays.
  • Two (2) Types of Collections:
    • Standard Collections: Found in the System.Collections namespace.
    • Generic Collections: Found in the System.Collections.Generic namespace.

System.Collections Namespace

  • ArrayList:
    • Known for maintaining an ordered collection of objects.
    • Unlike standard arrays that are static in size, an ArrayList is dynamic because items can be added and removed from specified index locations.
  • Hashtable:
    • Stores key/value pairs where the key represents the value in the collection.
    • Accessing values in a Hashtable requires specifying the corresponding key.
  • SortedList:
    • Represents a combination of ArrayList and Hashtable.
    • Stores key/value pairs where the key values sort the values.
    • Accessing values requires specifying either the key or the index value.
    • Properties:
    • Capacity: Gets or sets the capacity of the SortedList.
    • Count: Gets the count of the number of elements in the SortedList.
    • Item: Gets and sets the value associated with a specific key in the SortedList.
    • Keys: Carries the keys in the SortedList.
    • Values: Carries the values in the SortedList.
    • Methods:
    • void Add(object key, object value): Adds an item with the specified key and value into the SortedList.
    • void Clear(): Removes all items from the SortedList.
    • bool ContainsKey(object key): Returns the Boolean value true if the SortedList contains the specified key.
    • bool ContainsValue(object value): Returns the Boolean value true if the SortedList contains the specified value.
    • object GetByIndex(int index): Returns the value at the specified index.
    • object GetKey(int index): Returns the key at the specified index.
    • void Remove(object key): Removes the element corresponding to the specified key in the SortedList.
    • void RemoveAt(int index): Removes the element at the specified index in the SortedList.
  • Stack:
    • Represents a Last In, First Out (LIFO) collection of objects.
    • Uses push and pop operations for inserting and removing objects.
  • Queue:
    • Represents a First In, First Out (FIFO) collection of objects.
    • Uses enqueue and dequeue operations for adding and removing objects.

System.Collections Interfaces

  • IEnumerable:
    • An interface that allows looping through elements in a collection.
  • ICollection:
    • An interface that allows determining the number of elements in a collection and copying them into a simple array type.
  • IDictionary:
    • An interface that provides a list of elements accessible via a key or value rather than an index.

Overview of Generics

  • Concept of Generics:
    • Makes code reusable across different types by creating a template containing placeholder types.
    • Allows specifying the data types of programming elements when they are actually used in a program.
    • Found within the System.Collections.Generic namespace.
    • Enables creating collections such as linked lists, hashtables, stacks, and queues that operate on items of any data type.
    • Avoids the need to create custom collections for each type in an application.

System.Collections.Generic Namespace

  • List:
    • A generic collection providing an efficient and dynamically allocated array, commonly used to store a list of duplicate objects.
    • The List<T> class can grow and shrink dynamically with the number of objects.
    • Syntax: List<T> variableName = new List<T>();
    • Methods:
    • Add(): Used to add items to the end of a list. Elements can be accessed by calling their index value.
      • Example:
      • nameOfStud.Add("Rose");
      • Console.WriteLine(nameOfStud[2]);
    • Remove(): Removes the specified item from the list object. If duplicate data exists, it removes the first matching instance.
      • Example: nameOfStud.Remove("Mike");
    • IndexOf(): Checks for a specified element in the list object. Returns its index value if found; otherwise, returns -1.
      • Example: Console.WriteLine(nameOfStud.IndexOf("Rose"));
    • Sort(): Used to sort the elements in the list object.
      • Example: nameOfStud.Sort();
  • Queue:
    • A generic collection representing a First In, First Out (FIFO) collection of objects.
    • Syntax: Queue<int> ageQueue = new Queue<int>();
    • Methods:
    • Enqueue(): Adds an element to the end of the queue.
      • Example: ageQueue.Enqueue(18);
    • Peek(): Returns the element at the beginning of the queue without removing it.
      • Example: ageQueue.Peek();
    • Dequeue(): Removes and returns the value at the beginning of the queue.
      • Example: int getValue = ageQueue.Dequeue();
  • Stack:
    • A generic collection representing a Last In, First Out (LIFO) collection of instances.
    • Syntax: Stack<int> ageStack = new Stack<int>(20);
    • Methods:
    • Push(): Adds an element at the top of the stack.
      • Example: ageStack.Push(18);
    • Peek(): Returns the element at the top of the stack without removing it.
      • Example: int getValue = ageStack.Peek();
    • Pop(): Removes and returns the value at the top of the stack.
      • Example: int getValue = ageStack.Pop();

Bibliographic References

  • Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program (5th ed.). USA: Pearson Education, Inc.
  • Doyle, B. (2015). C# programming: from problem analysis to program design (5th ed.). Boston, MA: Cengage Learning.
  • Gaddis, T. (2016). Starting out with Visual C# (4th ed.). USA: Pearson Education, Inc.
  • Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.
  • Miles, R. (2016). Begin to code with C#. Redmond Washington: Microsoft Press.