⚠️ Malware / Trojaner / VirenGuardBreaker: Derailing AI-assisted malware analysis with a code comment(10.09.2026 um 11:00 Uhr)
⚠️ Malware / Trojaner / VirenAttack hides malware in PNGs and drops custom reverse tunnel on victims' machines(31.08.2026 um 20:26 Uhr)
⚠️ Malware / Trojaner / Viren33-hour BGP hijack of Softaculous traffic prompts security scramble(01.09.2026 um 14:04 Uhr)
🕵️ SicherheitslückenProlific Microsoft 0-day hunter drops CrowdStrike Falcon exploit PoC(03.09.2026 um 20:08 Uhr)
🔧 AI Nachrichten OpenAI commits $1B in AI credits to frontline cyber defenders(04.09.2026 um 01:47 Uhr)
⚠️ Malware / Trojaner / VirenGuardBreaker: Derailing AI-assisted malware analysis with a code comment(10.09.2026 um 11:00 Uhr)
⚠️ Malware / Trojaner / VirenAttack hides malware in PNGs and drops custom reverse tunnel on victims' machines(31.08.2026 um 20:26 Uhr)
⚠️ Malware / Trojaner / Viren33-hour BGP hijack of Softaculous traffic prompts security scramble(01.09.2026 um 14:04 Uhr)
🕵️ SicherheitslückenProlific Microsoft 0-day hunter drops CrowdStrike Falcon exploit PoC(03.09.2026 um 20:08 Uhr)
🔧 AI Nachrichten OpenAI commits $1B in AI credits to frontline cyber defenders(04.09.2026 um 01:47 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Part 8: Collections in C#

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Collections in C# provide powerful tools for managing and organising data efficiently. They enable developers to handle groups of objects with ease, whether you need fixed-size arrays or dynamic and flexible data structures like lists and dictionaries. In this article, we'll explore the primary types of collections in C#, their methods, and practical use cases.






1. Types of Collections in C






1.1 Arrays



An array is a fixed-size collection of elements of the same type. It’s useful for scenarios where the number of items is known and does not change.






Example:






CODE
int[] numbers = {1, 2, 3, 4, 5};

foreach (int number in numbers)
{
Console.WriteLine(number);
}









Key Methods and Properties:





  • Length: Returns the number of elements.


  • Array.Sort(array): Sorts the elements.


  • Array.Reverse(array): Reverses the order of elements.






1.2 Lists



A List is a dynamic collection that can grow or shrink in size. Lists are part of the System.Collections.Generic namespace and are more versatile than arrays.






Example:






CODE
List<string> names = new List<string> {"John", "Adrian", "Charlie"};

names.Add("Adrian");

foreach (string name in names)
{
Console.WriteLine(name);
}









Key Methods:





  • Add(item): Adds an item to the list.


  • Remove(item): Removes the first occurrence of an item.


  • Contains(item): Checks if an item exists in the list.









1.3 Dictionaries



A Dictionary is a collection of key-value pairs, where each key is unique. Dictionaries are ideal for scenarios where quick lookups are needed.






Example:






CODE
Dictionary<int, string> students = new Dictionary<int, string>
{
{1, "Adrian"},
{2, "John"},
{3, "Charlie"}
};

if (students.ContainsKey(2))
{
Console.WriteLine($"Student ID 2: {students[2]}");
}









Key Methods:





  • Add(key, value): Adds a new key-value pair.


  • Remove(key): Removes an item by key.


  • ContainsKey(key): Checks if a specific key exists.



Performance Note: Dictionaries provide fast lookups due to their hashing mechanism, making them suitable for scenarios where keys are frequently searched.






1.4 Sets



A HashSet is a collection that contains unique elements and does not allow duplicates. It’s optimised for fast lookups.






Example:






CODE
HashSet<int> uniqueNumbers = new HashSet<int> {1, 2, 3, 3, 4};

foreach (int number in uniqueNumbers)
{
Console.WriteLine(number);
}









Key Methods:





  • Add(item): Adds an item if it doesn’t already exist.


  • Remove(item): Removes an item.


  • Contains(item): Checks if an item exists.



Performance Note: Use HashSet when you need to ensure uniqueness in your collection.






2. Methods of Collections and Their Uses






Common Methods Across Collections:



(LINQ will be discussed in the following articles)





  • Iteration: Use foreach to iterate through items in a collection.


  • Sorting: Use Sort() in lists or OrderBy() in LINQ for advanced sorting.


  • Filtering: LINQ methods like Where() allow filtering based on conditions.


  • Conversion: Convert between collections using ToArray(), ToList(), etc.






Example of LINQ Filtering:






CODE
List<int> numbers = new List<int> {1, 2, 3, 4, 5};

var evenNumbers = numbers.Where(n => n % 2 == 0);

foreach (int number in evenNumbers)
{
Console.WriteLine(number);
}









3. Practical Example: Data Management with Collections






Scenario: Managing a Student Roster



This example demonstrates how to use multiple types of collections to manage student data.






Example:






CODE
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// Store student data
Dictionary<int, string> students = new Dictionary<int, string>
{
{1, "Adrian"},
{2, "John"},
{3, "Charlie"}
};

// Add new students
students.Add(4, "Diana");

// All students
Console.WriteLine("Student Roster:");
foreach (var student in students)
{
Console.WriteLine($"ID: {student.Key}, Name: {student.Value}");
}

// Check if a student exists
if (students.ContainsKey(2))
{
Console.WriteLine("Student ID 2 is in the roster.");
}

// Remove a student
students.Remove(3);
Console.WriteLine("Updated Roster:");
foreach (var student in students)
{
Console.WriteLine($"ID: {student.Key}, Name: {student.Value}");
}
}
}

/*
Student Roster:
ID: 1, Name: Adrian
ID: 2, Name: John
ID: 3, Name: Charlie
ID: 4, Name: Diana
Student ID 2 is in the roster.
Updated Roster:
ID: 1, Name: Adrian
ID: 2, Name: John
ID: 4, Name: Diana
*/










4. Advanced Topics






Thread-Safe Collections



For applications requiring thread-safe operations, consider using ConcurrentDictionary or other thread-safe collections from the System.Collections.Concurrent namespace.






Example:






CODE
using System;
using System.Collections.Concurrent;

class Program
{
static void Main()
{
ConcurrentDictionary<int, string> safeDict = new ConcurrentDictionary<int, string>();

safeDict.TryAdd(1, "Adrian");
safeDict.TryAdd(2, "John");

foreach (var item in safeDict)
{
Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
}
}
}









Immutable Collections



To ensure the collection cannot be modified after creation, use immutable collections from System.Collections.Immutable.






Example:






CODE
using System.Collections.Immutable;

ImmutableList<string> immutableNames = ImmutableList.Create("Adrian", "John", "Charlie");

foreach (var name in immutableNames)
{
Console.WriteLine(name);
}


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
GuardBreaker: Derailing AI-assisted malware analysis with a code comment
1 Quelle
Attack hides malware in PNGs and drops custom reverse tunnel on victims' machines
1 Quelle
33-hour BGP hijack of Softaculous traffic prompts security scramble
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Part 8: Collections in C#

Thematisch verwandte Begriffe: Part, Collections · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...