Udostępnij za pośrednictwem


X++, C# Comparison: Collections of Keys with Values

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 Map collection class. The Map collection holds pairs of values, the key value plus a data value. This resembles the .NET Framework class named System.Collections.Generic.Dictionary.

X++ to C# Comparisons

There are similarities and differences in how a key-value collection is implemented in X++ and C#.

Cc967409.collapse_all(en-us,AX.60).gifSimilarities

The following list describes similarities between X++ and C# regarding their collections that store key-value pairs:

  • Both prevent duplicate keys.

  • Both use an enumerator (or iterator) to loop through the items.

  • Both key-value collection objects are constructed with designations of the types that are stored as key and value.

  • Both can store class objects, and are not limited to storing primitives like int.

Cc967409.collapse_all(en-us,AX.60).gifDifferences

The following table describes differences between X++ and C# regarding their collections classes that store key-value pairs:

Feature

X++

C#

Comments

Duplicate keys

In X++ the Map class prevents duplicate keys by implicitly treating your call to its insert method as an operation to update only the value associated with the key.

In C# the Dictionary class throws an exception when you try to add a duplicate key.

Duplicate keys are prevented in both languages, although by different techniques.

Delete items

In X++ the delete method on an iterator object is used to remove an unwanted key-value pair from a Map.

In C# the Dictionary class has a remove method.

In both languages, an enumerator is made invalid if the collection item count is modified during the life of the enumerator.

Example 1: Declaration of a Key-Value Collection

In both languages, the type of items that the key-value collection stores must be specified. In X++ the type is specified at time of construction. In C# the type is specified at both the time of declaration and the time of construction. Code examples are in the following table.

X++

C#

Map mapKeyValue;

MapEnumerator enumer;

MapIterator mapIter;

SysCollGen.Dictionary

<int,string>

dictKeyValue;

SysCollGen.IEnumerator

<SysCollGen.KeyValuePair

<int,string>> enumer;

SysCollGen.KeyValuePair

<int,string>

kvpCurrentKeyValuePair;

Example 2: Construction of the Collection

In both languages, the type of items that the key-value collection stores specified during 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#

mapKeyValue =

new Map

(Types::Integer

,Types::String);

dictKeyValue = new

SysCollGen.Dictionary

<int,string>();

Example 3: Add an Item to the Collection

There is almost no difference in how an item is added to a key-value collection, in X++ and C#. Code examples are in the following table.

X++

C#

mapKeyValue.insert

(xx ,int2str(xx)

+ "_Value");

dictKeyValue.Add

(xx ,xx.ToString()

+ "_Value");

Example 4: Iterate Through a Key-Value Collection

Enumerators are used to loop through the key-value collections in both X++ and C#. Code examples are in the following table.

X++

C#

enumer = mapKeyValue.getEnumerator();

while (enumer.moveNext())

{

iCurrentKey = enumer.currentKey();

sCurrentValue = enumer.currentValue();

// Display key and value here.

}

enumer = dictKeyValue

.GetEnumerator();

while (enumer.MoveNext())

{

kvpCurrentKeyValuePair = enumer.Current;

// Display .Key and .Value properties

// of kvpCurrentKeyValuePair here.

}

Example 5: Update the Value Associated with a Key

The syntax is very different between the two languages for an update of the value associated to a given key. Code examples for the key 102 are in the following table.

X++

C#

mapKeyValue.insert

(102 ,".insert(), Re-inserted"

+ " key 102 with a different value.");

dictKeyValue[102] =

"The semi-hidden .item property"

+ " in C#, Updated the value for key 102.";

Example 6: Delete One Item

The syntax is very different between the two languages to delete one key-value pair from a collection, while iterating through the collection members. Code examples for the key 102 are in the following table.

X++

C#

mapIter = new MapIterator

(mapKeyValue);

//mapIter.begin();

while (mapIter.more())

{

iCurrentKey = mapIter.key();

if (104 == iCurrentKey)

{

// mapKeyValue.remove would invalidate the iterator.

mapIter.delete();

break;

}

mapIter.next();

}

dictKeyValue

.Remove(104);

See also

X++, C# Comparisons

X++, C# Comparison: Collections

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