X++, C# Comparison: Collections [AX 2012]
Updated: June 9, 2009
Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
Microsoft Dynamics AX provides the X++ List collection class. The .NET Framework that is used in C# has a similar class named System.Collections.Generic.List.
The following table compares methods on the X++ List class to the methods on System.Collections.Generic.List from the .NET Framework and C#.
| Feature | X++ | C# | Discussion |
|---|---|---|---|
| Declaration of collection | List myList; | List<string> myList; | The X++ declaration does not include the type of elements to be stored. |
| Declaration of iterator |
| IEnumerator<string> iter; | In X++ the ListIterator object has methods that can insert and delete items from the List. The X++ ListEnumerator cannot modify the contents of the List. In X++ the ListEnumerator object is always created on the same tier as the List. This is not always true for ListIterator. |
| Obtaining an iterator |
| myList.GetEnumerator() | In both X++ and C#, the List object has a getter method for an associated enumerator. |
| Constructor | new List(Types::String) | new List<string>() | Information about the type of objects to be stored inside the List classes is given to the constructor in both X++ and C#. |
| Updating data |
| Enumerator – the enumerator becomes invalid if any items in the List are added or removed. | Enumerators become invalid after items are added or deleted from the List, in both X++ and C#. |
| Updating data | In X++ the List class has methods for adding items at the start or end of the list. | In C# the List class has methods for adding members at any position in the list. It also has methods for removing items from any position. | In X++ items can be removed from the List only by an iterator. |
The following table displays code examples in X++ and C# that declare List collections.
| X++ | C# |
|---|---|
| List listStrings ,list2 ,listMerged; ListIterator literator; | using System; using SysCollGen = System.Collections.Generic; SysCollGen.List<string> listStrings ,list2 ,listMerged; SysCollGen.IEnumerator<string> literator; |
In both languages, the type of items that the collection stores must be specified at the time of construction. For class types, X++ can get no more specific than whether the type is a class (Types::Class). Code examples are in the following table.
| X++ | C# |
|---|---|
| listStrings = new List( Types::String ); | listStrings = new SysCollGen.List<string>(); |
In both X++ and C#, the collection provides a method for appending an item to the end of the collection, and for inserting an item the start.
In C# the collection provides a method for inserting at any point in the collection based on an index value. In X++ a collection iterator can insert an item at its current position.
Code examples are in the following table.
| X++ | C# |
|---|---|
| listStrings.addEnd ("String_BB."); listStrings.addStart ("String_AA."); | listStrings.Add ("String_BB."); listStrings.Insert (0 ,"String_AA."); |
| // Iterator performs a midpoint // insert at current position. listIterator.insert ("dog"); | // Index 7 determines the insertion point. listStrings.Insert (7 ,"dog"); |
Both X++ and C# have iterator classes that you can use to step through the items in a collection. Code examples are in the following table.
| X++ | C# |
|---|---|
| literator = new ListIterator (listStrings); // Now the iterator points at the first item. | literator = listStrings .GetEnumerator(); // Now enumerator points before // the first item, not at // the first item. |
| // The more method answers whether // the iterator currently points // at an item. while (literator.more()) { info(any2str (literator.value())); literator.next(); } | // The MoveNext method both // advances the item pointer, and // answers whether the pointer is // pointing at an item. while (literator.MoveNext()) { Console.WriteLine (literator.Current); } |