ArrayList, List and Dictionary are types of collections within C#

ArrayList, List and Dictionary are types of collections within C# for holding data and then manipulated. How to Use each with an Example:



1. ArrayList



The non-generic type of ArrayList that can be used with the elements of any kind of type; it falls in the category of the namespace System.Collections but since this one is not a type-safe means it is often superseded in modern C# by the List of T


Example

using System;

using System.Collections;

class Program

static void Main()

{

ArrayList arrayList = new ArrayList();


// Adding elements of different types

arrayList.Add(1);

arrayList.Add("Hello");

arrayList.Add(3.14);

// Accessibility to elements'

foreach (var item in arrayList)

{

Console.WriteLine(item);

// Remove an element

arrayList.Remove("Hello");

Console.WriteLine("After Removal:");

foreach (var item in arrayList)

{

Console.WriteLine(item);

}

}

}


Key Points:


Not type-safe (can store any object type).

Slower than generic collections due to boxing/unboxing.

Requires System.Collections namespace.


2. List<T>



List<T> is a generic collection that allows storing elements of a specified type. It is part of the System.Collections.Generic namespace and is more efficient and type-safe than ArrayList.


Example:

using System;

using System.Collections.Generic;

class Program

{

static void Main()

{

List<int> numbers = new List<int>();

// Adding elements

numbers.Add(1);

numbers.Add(2);

numbers.Add(3);

// Accessing elements

foreach (int number in numbers)

{

Console.WriteLine(number);

}


// Removing an element

numbers.Remove(2);

Console.WriteLine("After Removal:");

foreach (int number in numbers)

{

Console.WriteLine(number);

}

// Inserting at a specific position

numbers.Insert(1, 99);

Console.WriteLine("After Inserting 99:");

foreach (int number in numbers)

{

Console.WriteLine(number);

}

}

}


Key Points:


Type-safe (stores a specific type).

Faster and more efficient than ArrayList.

Requires System.Collections.Generic namespace.

3. Dictionary<TKey, TValue>


Dictionary<TKey, TValue> It is a collection that holds pair of key and value that has unique key. Along with this, it is included in the namespace of the System.Collections.Generic.

Below is an example:

using System;

using System.Collections.Generic;

class Program

{

static void Main()

{

// Creating a dictionary with string keys and int values

Dictionary<string, int> ages = new Dictionary<string, int>();


// Adding key-value pairs

ages.Add("Alice", 25);

ages.Add("Bob", 30);

ages["Charlie"] = 35; // Alternative way to add

// Accessing values by key

Console.WriteLine($"Alice's age: {ages["Alice"]}");

// Iterating through key-value pairs

foreach (KeyValuePair<string, int> kvp in ages)

{

Console.WriteLine($"{kvp.Key}: {kvp.Value}");

}


// Checking if a key exists

if (ages.ContainsKey("Bob"))

{

Console.WriteLine("Bob is in the dictionary.");

}


// Removing a key-value pair

ages.Remove("Alice");

Console.WriteLine("After Removing Alice:");

foreach (var kvp in ages)

{

Console.WriteLine($"{kvp.Key}: {kvp.Value}");

}

}

}




Key Points:


Stores unique keys with associated values.

Fast lookups by key.

Type-safe and efficient.

Requires System.Collections.Generic namespace.


Summary Table


These collections vary in functionality based on your need for type safety, performance, and structure requirements.

Conclusion


Selecting the correct collection depends on your particular use case with C#:


ArrayList will work well with older code or when you need to have a mixed-type of collection. In general though, it's avoided when you can work with one of the newer, safer collections.


List<T> would be the first choice among collections for lists of elements that are of a certain type. It provides type safety, better performance, and even flexibility for most use scenarios.


Dictionary<TKey, TValue> is ideal when data needs to be stored along with quick retrieval using some unique keys, making key-based lookup important in cases.



By knowing such collections and their differences, one can write more efficient C# code that is type safe and maintainable.
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.