In Dynamics 365 Finance and Operations (D365FO) X++, collection classes are special framework classes that allow you to store and manipulate groups of objects or values (similar to Lists, Sets, Maps in other languages).
They’re especially useful when you need in-memory collections instead of database tables for temporary processing.
|
Class |
Decision Flow |
Performance & Notes |
Purpose |
Example |
|
List |
order + duplicates → List |
Fast sequential access, but slow if you frequently check
for existence (exists() is O(n)) |
Stores an ordered collection of elements, allows
duplicates. |
List list = new List(Types::Integer); |
|
Set |
unique values → Set |
Optimized for existence checks (O(1) on average). No
duplicates allowed |
Stores unique elements, unordered, no duplicates. |
Set mySet = new Set(Types::String); |
|
Map |
key-value mapping → Map |
Fast lookups by key (O(1) average), best for associative
arrays. |
Stores key-value pairs (dictionary-style), keys must be
unique. |
Map myMap = new Map(Types::String, Types::Integer); |
|
Struct |
dynamic fields like a record → Struct |
Flexible, but less performant for very large data sets
(field names are strings) |
Key-value store but keys are field names (like a
record without a table). |
Struct s = new Struct(); |
|
Array (Array class,not native X++ arrays) |
fixed-size index access → Array |
Very fast for fixed-size, index-based operations. Must
predefine capacity. |
Fixed-size sequential collection. |
Array arr = new Array(Types::Real); |
X++ Collection Classes Syntax
🔹 1. List – Ordered, Allows Duplicates
Purpose: Ordered collection, allows duplicates.
Create:
List myList = new List(Types::Integer);
Common Methods:
addStart(val), addEnd(val), insert(index, val), remove(val), exists(val), elements(), getEnumerator()
Example:
List l = new List(Types::String); l.addEnd("Apple"); l.addStart("Banana"); ListEnumerator le = l.getEnumerator(); while (le.moveNext()) info(le.current());💡 Tip: Use .exists(value) if you just need to check for a value.
🔹 2. Set – Unique, Unordered
Purpose: Unordered, unique items only.
Create:
Set mySet = new Set(Types::String);
Common Methods:
add(val), remove(val), in(val), elements(), getEnumerator()
Example:
Set s = new Set(Types::Integer); s.add(10); s.add(20); s.add(10); // duplicate ignored if (s.in(20)) info("20 exists"); SetEnumerator se = s.getEnumerator(); while (se.moveNext()) info(strFmt("%1", se.current()));💡 Tip: Use .in(value) for quick existence checks.
🔹 3. Map – Key-Value Pairs
Purpose: Key-Value storage, unique keys.
Create:
Map myMap = new Map(Types::String, Types::Real);
Common Methods:
insert(key, val), remove(key), exists(key), lookup(key), elements(), getEnumerator()
Example:
Map m = new Map(Types::String, Types::Integer); m.insert("Pen", 10); m.insert("Book", 25); MapEnumerator me = m.getEnumerator(); while (me.moveNext()) info(strFmt("%1 -> %2", me.currentKey(), me.currentValue()));Direct lookup by keyreal price = myMap.lookup("Pen"); info(strFmt("Pen price: %1", price));
🔹 4. Array – Fixed-Size Indexed
Purpose: Fixed-size, index-based collection.
Create:
Array arr = new Array(Types::Integer);
Common Methods:
value(index, val), lastIndex(), exists(index)
Example:
Array a = new Array(Types::Integer); a.value(1, 100); a.value(2, 200); for (int i = 1; i <= a.lastIndex(); i++) info(strFmt("Index %1: %2", i, a.value(i)));
🔹 5. Struct – Named Fields
Purpose: Dynamic field-value pairs (like a record without a table).
Create:
Struct s = new Struct();
Common Methods:
add(fieldName, val), value(fieldName), fields(), fieldName(index)
Example:
Struct st = new Struct();st.add("Name", "Person1"); st.add("Age", 30);
info(st.value("Name"));