Using Collections

How to use List<T>, Dictionary<K,V>, and other generic collections.

Includes

#include <System/Collections/Generic/List.hpp>
#include <System/Collections/Generic/Dictionary.hpp>
#include <System/Collections/Generic/HashSet.hpp>
#include <System/Collections/Generic/Queue.hpp>
#include <System/Collections/Generic/Stack.hpp>

List<T>

Wraps std::vector<T>. Use when you need a .NET-compatible resizable list.

System::Collections::Generic::List<std::string> names;
names.Add("Alice");
names.Add("Bob");
names.Add("Carol");

// Access by index
std::string first = names[0];           // "Alice"

// Iterate (range-for supported)
for (const auto& name : names) {
    std::cout << name << "\n";
}

// Remove
names.Remove("Bob");

// Contains
bool has = names.Contains("Alice");     // true

// Count
int n = names.Count();                  // 2

// Convert to std::vector
std::vector<std::string> vec = names.ToVector();

Dictionary<K,V>

Wraps std::unordered_map<K,V>.

System::Collections::Generic::Dictionary<std::string, int> scores;
scores["Alice"] = 100;
scores["Bob"] = 85;

// Access
int s = scores["Alice"];                // 100

// Check key exists
bool has = scores.ContainsKey("Carol"); // false

// Remove
scores.Remove("Bob");

// Iterate
for (const auto& [key, value] : scores) {
    std::cout << key << ": " << value << "\n";
}

// TryGetValue
int val;
if (scores.TryGetValue("Alice", val)) {
    std::cout << "Found: " << val << "\n";
}

HashSet<T>

Wraps std::unordered_set<T>.

System::Collections::Generic::HashSet<int> seen;
seen.Add(1);
seen.Add(2);
seen.Add(1);  // duplicate — ignored

bool contains = seen.Contains(2);       // true
int count = seen.Count();               // 2

Queue<T>

FIFO queue backed by std::queue.

System::Collections::Generic::Queue<std::string> q;
q.Enqueue("first");
q.Enqueue("second");

std::string front = q.Dequeue();        // "first"
std::string peek  = q.Peek();           // "second" (doesn't remove)

Stack<T>

LIFO stack backed by std::stack.

System::Collections::Generic::Stack<int> stack;
stack.Push(10);
stack.Push(20);

int top = stack.Pop();                  // 20
int peek = stack.Peek();               // 10

Choosing the Right Collection

C# typesharp-runtime typeUnderlying
List<T>List<T>std::vector
Dictionary<K,V>Dictionary<K,V>std::unordered_map
HashSet<T>HashSet<T>std::unordered_set
Queue<T>Queue<T>std::queue
Stack<T>Stack<T>std::stack
SortedDictionary<K,V>SortedDictionary<K,V>std::map
LinkedList<T>LinkedList<T>std::list
T[] (simple)std::vector<T>direct