Skip to content

Collections & LINQ

NexusScript ships the everyday .NET collections and a broad slice of LINQ. You write them with normal C# syntax — list.Add(x), list[i], foreach, dict[key] — and the compiler maps that to the engine bindings. This page is about what's available and the few gotchas.

List

A growable array. The workhorse — rosters, spawn pools, queues of work.

List<string> names = new List<string>();
names.Add("Ada");
names.Insert(0, "Grace");
string first = names[0];          // indexer
names[0] = "Grace H.";            // set
int n = names.Count;
bool has = names.Contains("Ada");
names.Remove("Ada");
names.RemoveAt(0);
foreach (string s in names) Debug.Log(s);
names.Sort();
names.Reverse();
string[] arr = names.ToArray();

Add · Insert · Remove · RemoveAt · Clear · Contains · IndexOf · LastIndexOf · Find · FindIndex · Sort · Reverse · ToArray · Count · [i].

Dictionary

Key → value lookups (scores by player id, config by name).

Dictionary<string, int> score = new Dictionary<string, int>();
score["ada"] = 10;                       // add/overwrite
score["ada"] += 5;
if (score.ContainsKey("ada")) { }
int v;
if (score.TryGetValue("ada", out v)) Debug.Log(v);   // safe read
score.Remove("ada");
foreach (string key in score.Keys) Debug.Log(key + " = " + score[key]);

[key] get/set · Add · Remove · Clear · ContainsKey · ContainsValue · TryGetValue · Keys · Values · Count.

TryGetValue over [key] for reads

Reading a missing key with dict[key] throws. Use TryGetValue (or guard with ContainsKey) when the key might not be present.

HashSet, Queue & Stack

HashSet<string> seen = new HashSet<string>();
if (seen.Add(playerId)) FirstVisit(playerId);   // Add returns false if already present
seen.UnionWith(other); seen.IntersectWith(other); // set algebra

Queue<GameObject> spawnQueue = new Queue<GameObject>();
spawnQueue.Enqueue(obj);
GameObject next = spawnQueue.Dequeue();          // FIFO
GameObject peek = spawnQueue.Peek();

Stack<string> history = new Stack<string>();
history.Push("room1");
string back = history.Pop();                     // LIFO
  • HashSet — unique membership: Add · Remove · Contains · UnionWith · IntersectWith · ExceptWith · IsSubsetOf · Overlaps · Count.
  • Queue (FIFO) — Enqueue · Dequeue · Peek · Contains · Clear · Count.
  • Stack (LIFO) — Push · Pop · Peek · Contains · Clear · Count.

LINQ

Query and transform any collection or array with the familiar LINQ methods (and lambdas):

using System.Linq;

string[] players = Networking.GetPlayers();

var nearby = players
    .Where(id => Player.IsInRange(myId, id, 10f))
    .OrderBy(id => Player.GetDistanceTo(myId, id))
    .ToList();

int total = score.Values.Sum();
bool anyClose = players.Any(id => Player.IsInRange(myId, id, 2f));
string closest = nearby.FirstOrDefault();        // null/"" if none
int alive = enemies.Count(e => e.activeSelf);

Available: Where · Select · SelectMany · OrderBy / OrderByDescending · First / FirstOrDefault · Last / LastOrDefault · Single / SingleOrDefault · Any · All · Count · Sum · Average · Min · Max · Take / Skip · TakeWhile / SkipWhile · Distinct · Union / Intersect / Except · Contains · OfType · ToArray · ToList.

Things to remember from the language subset

  • No tuples — return a small class/struct or use an out param for multiple values (see Basics).
  • struct is a reference type here — storing the same struct in two list slots shares it; reassign with new for an independent copy.
  • LINQ that materializes (ToList, OrderBy, Sum) allocates — fine for menus and turn‑based logic; in a hot Update loop, prefer a plain for over re‑querying every frame.

Quick reference

Type Calls
List<T> Add · Insert · Remove · RemoveAt · Clear · Contains · IndexOf · Find · Sort · Reverse · ToArray · Count · [i]
Dictionary<K,V> [key] · Add · Remove · ContainsKey · TryGetValue · Keys · Values · Count
HashSet<T> Add · Remove · Contains · UnionWith · IntersectWith · ExceptWith · Count
Queue<T> / Stack<T> Enqueue/Dequeue/Peek · Push/Pop/Peek
LINQ Where · Select · OrderBy · First/OrDefault · Any · All · Count · Sum · Take/Skip · Distinct · ToList

→ Back to Basics for the full language subset, or Storage to persist what you collect.