Udostępnij za pośrednictwem


X++, C# Comparison: Collections

Applies To: Microsoft Dynamics AX 2012 R3, 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.

Comparing the Use of the List Classes

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

  • ListIterator iter;

  • ListEnumerator enumer;

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

  • new ListIterator (myList)

  • myList.getEnumerator()

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.

  • Iterator – the iterator has methods that insert and delete items from the List. The iterator remains valid.

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.

Example 1: Declaration of a List

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;

Example 2: Construction of a List

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>();

Example 3: Add Items to a List

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");

Example 4: Iterate Through a List

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);

}

Example 4b: foreach in C#

In C# the foreach keyword is often used to simplify the task of iterating through a list. The following code example behaves the same as the previous C# example.

foreach (string currentString in listStrings)

{

Console.WriteLine(currentString);

}

Example 5: Delete the Second Item

The following table contains code examples that delete the second item from the collection. In X++ this requires an iterator. In C# the collection itself provides the method for removing an item.

X++

C#

literator.begin();

literator.next();

literator.delete();

listStrings.RemoveAt(1);

Example 6: Combine Two Collections

The following table contains code examples that combine the contents of two collections into one.

X++

C#

listStrings = List::merge

(listStrings ,listStr3);

// Or use the .appendList method:

listStrings.appendList (listStr3);

listStrings.InsertRange

(listStrings.Count ,listStr3);

See also

X++, C# Comparisons

Arrays

Containers

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.