List Collection in C#

List Collection

  • Lists are similar to arrays but have a dynamic size, unlike the static size of arrays.
  • Lists are useful when the size of the collection needs to change during the program's execution by adding or removing items.
  • Lists in C# are similar to Python lists but can only contain values of one specified type.

Declaring a List

  • Declaration differs from arrays; the type must start with an uppercase 'L'.
  • Specify the element type the list will contain using less than and greater than symbols (e.g., <string>, <int>, <double>).
  • Define the list identifier (name).
  • Examples:
    • List<string> stringList;
    • List<int> intList;
    • List<double> doubleList;

Initializing a List

  • Use the new keyword to create a list.
  • Syntax: new List<ElementType>();
  • Declaration and assignment can be on separate lines or combined into a single line.
  • Example:
    • List<string> myList = new List<string>();

Printing List Type

  • Using Console.WriteLine() on a list will print out the type of the list.
  • The output will be an abstract representation of the list's type.

Adding Elements to a List

  • Elements are added using methods instead of index positions, which aligns with object-oriented programming.
  • Use the Add method to append elements to the end of the list.
  • Syntax: listName.Add(element);
  • The element must match the declared type of the list.
  • Variables, constants, or literals can be added.
  • Duplicate elements can be added to the list.
  • Example:
List<string> names = new List<string>();
names.Add("Nala");
names.Add("Simon");
names.Add("Sam");
names.Add("Simon");

Removing Elements from a List

  • Use the Remove method to remove the first occurrence of a specified element.
  • Syntax: listName.Remove(element);
  • If the element is not in the list, the list remains unchanged.
  • Example:
names.Remove("Nala"); // Nala is removed
names.Remove("Simon"); // First occurrence of Simon is removed
names.Remove("SAM");  // No effect as SAM is not in the list

Absence Testing with the 'Count' Property

  • Absence testing checks for empty collections, which is different from invalid testing (where inputs are invalid).
  • The Count property returns the number of elements in the list.
  • Useful for preventing errors, such as division by zero when calculating averages.
  • Example:
if (myList.Count == 0)
{
    Console.WriteLine("Warning: The list is empty.");
}
  • In the context of average calculation:
    • The average is obtained by the sum of elements divided by their count.
    • Average=<em>i=1nx</em>inAverage = \frac{\sum<em>{i=1}^{n} x</em>i}{n}, where nn is the number of elements.
    • If n=0n = 0, a division by zero error occurs.

Other List Methods

  • Sort(): Sorts the list.
  • Reverse(): Reverses the order of elements in the list.
  • IndexOf(value): Returns the index of a given value.
  • ToArray(): Converts the list into an array.
  • Contains(element): Returns true if the list contains the specified element.

Summary

  • Lists are dynamic collections, unlike arrays.
  • Elements are added using the Add method and removed using the Remove method.
  • The Count property is useful for absence testing and loop control.
  • Lists provide various methods for manipulation, similar to array methods.