Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

Collections in C#



Collections in C# are essential for managing groups of objects. The .NET Framework offers several types of collections, each with unique properties that make them suitable for various scenarios. Here are some of the most commonly used collections:

1. Array

  • Fixed size.

  • Homogeneous elements.

  • Provides fast access by index.

    csharp
    int[] numbers = new int[5];
    

2. List<T>

  • Dynamic size.

  • Strongly typed elements.

  • Better performance for adding and removing elements compared to arrays.

    csharp
    List<int> integers = new List<int>();
    integers.Add(1);
    integers.Add(2);
    

3. Dictionary<TKey, TValue>

  • A collection of key-value pairs.

  • Provides fast lookups by key.

  • Useful for associative arrays.

    csharp
    Dictionary<string, int> studentGrades = new Dictionary<string, int>();
    studentGrades["Alice"] = 90;
    studentGrades["Bob"] = 85;
    

4. HashSet<T>

  • An unordered collection of unique elements.

  • Provides set operations like union, intersection, and difference.

  • Ensures no duplicates.

    csharp
    HashSet<string> inventory = new HashSet<string> { "Apple", "Banana", "Orange" };
    

5. Queue<T>

  • First-In-First-Out (FIFO) collection.

  • Great for scenarios where order matters, such as task scheduling.

    csharp
    Queue<string> tasks = new Queue<string>();
    tasks.Enqueue("Task 1");
    tasks.Enqueue("Task 2");
    

6. Stack<T>

  • Last-In-First-Out (LIFO) collection.

  • Ideal for scenarios like undo mechanisms or expression evaluations.

    csharp
    Stack<string> history = new Stack<string>();
    history.Push("Page 1");
    history.Push("Page 2");
    

7. SortedList<TKey, TValue>

  • A combination of a List and a Dictionary.

  • Maintains elements in a sorted order by key.

    csharp
    SortedList<string, string> phoneBook = new SortedList<string, string>();
    phoneBook["Alice"] = "123-456-7890";
    phoneBook["Bob"] = "987-654-3210";
    

8. LinkedList<T>

  • A doubly linked list with pointers to the next and previous elements.

  • Efficient for insertions and deletions.

    csharp
    LinkedList<string> playlist = new LinkedList<string>();
    playlist.AddLast("Song 1");
    playlist.AddLast("Song 2");
    

9. ObservableCollection<T>

  • Monitors changes such as addition or removal of elements.

  • Useful for data binding in applications like WPF.

    csharp
    ObservableCollection<string> shoppingList = new ObservableCollection<string>();
    shoppingList.CollectionChanged += (sender, e) => Console.WriteLine("Collection changed!");
    shoppingList.Add("Milk");
    

10. Concurrent Collections

  • Thread-safe, designed for use in multi-threaded environments.

  • Includes ConcurrentDictionary, ConcurrentQueue, ConcurrentStack, and BlockingCollection.

    csharp
    ConcurrentQueue<int> concurrentQueue = new ConcurrentQueue<int>();
    concurrentQueue.Enqueue(1);
    concurrentQueue.Enqueue(2);
    

Collections make it easier to manage and manipulate grouped data efficiently within your applications. Each type has its strengths and use cases, depending on the requirements of your project. If you need more detailed information or examples about specific collections, just let me know!




All rights reserved | Privacy Policy | Sitemap