Compartilhar via


X++, C# Comparison: Array Syntax

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

This topic compares array syntax between X++ and C#.

X++ to C# Comparisons

There are similarities and differences in the features and syntax for arrays in X++ versus C#.

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

Overall there is much similarity in the syntax and treatment of arrays in X++ and C#. However there are many differences.

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

The following table lists areas in the [] syntax for arrays that are different for X++ and C#.

Category

X++

C#

Comments

Declaration

An array is declared with square brackets appended to the variable name.

An array is declared with square brackets appended to the data type.

int myInts[]; // X++

Note

An X++ array cannot be a parameter in a method.

Declaration

The array syntax supports only primitive data types, such as int and str. The syntax does not support classes or tables.

The array syntax supports primitive data types and classes.

In X++ you can use the Array Array for an array of objects.

Declaration

X++ is limited to single dimension arrays (myStrings[8]).

C# adds support for multi-dimensional arrays (myStrings[8,3]) and for jagged arrays (myStrings[8][3]).

In X++ you cannot have an array of arrays. However, there is advanced syntax for limiting the amount of active memory that a large array can consume, which looks like the multi-dimensional syntax in C#: int intArray[1024,16];. For more information, see Best Practice Performance Optimizations: Swapping Arrays to Disk.

Declaration

In X++ an array is a special construct but it is not an object.

In C# all arrays are objects regardless of syntax variations.

X++ does have an Array class, but its underlying mechanism differs from arrays created by using the [] syntax.

In C# all arrays use the same underlying mechanism, regardless of whether [] syntax of the System.Array class is used in your code.

Length

In X++ the length of a static sized array is determined in the declaration syntax.

In C# the size of an array is determined when the array object is constructed.

When you use the [] declaration syntax in X++, no more preparation is needed before you assign values to the array.

In C# you must declare and then construct the array before assigning to it.

Length

An X++ array can have a dynamic length that can be increased even after population has begun. This applies only when the array is declared without a number inside the []. Performance might be slowed if the length of the dynamic array is increased many times.

In C# the length of an array cannot be changed after the length is set.

In the following fragment of X++ code, only the myInts array is dynamic and can increase in size.

int myInts[];

int myBools[5];

;

myInts[2] = 12;

myInts[3] = 13;

myBools[6] = 26; //Error

Length

You can get the length of some arrays by using the dimOf function.

C# arrays are objects that have a Length property.

No comments.

Indexing

Array indexing is 1 based.

Array indexing is 0 based.

mtIntArray[0] would cause an error in X++.

Constant

In X++ a constant value is best achieved by using the #define precompiler directive.

In C# you can decorate your variable declaration with the keyword const, to achieve a constant value.

X++ has no const keyword. C# cannot assign values to variables that are created by its #define precompiler directive.

X++ and C# Samples

The following code samples show how arrays of primitive data types are handled. The first sample is in X++, and the second sample is in C#. Both samples achieve the same results.

Cc967423.collapse_all(en-us,AX.60).gifX++ Sample

    static void JobRs005a_ArraySimple(Args _args)
    {
        #define.macroArrayLength(3)
    
        // Static length.
        str sSports[#macroArrayLength];
    
        // Dynamic length, changeable during run time.
        int years[];
        int xx;
        Global::warning("-------- SPORTS --------");
        sSports[#macroArrayLength] = "Baseball";
    
        for (xx=1; xx <= #macroArrayLength; xx++)
        {
            info(int2str(xx) + " , [" + sSports[xx] + "]");
        }
        warning("-------- YEARS --------");
        years[ 4] = 2008;
        years[10] = 1930;
    
        for (xx=1; xx <= 10; xx++)
        {
            info(int2str(xx) + " , " + int2str(years[xx]));
        }
    }

Cc967423.collapse_all(en-us,AX.60).gifOutput

The output to the Infolog is as follows:

-------- SPORTS --------
1 , []
2 , []
3 , [Baseball]
-------- YEARS --------
1 , 0
2 , 0
3 , 0
4 , 2008
5 , 0
6 , 0
7 , 0
8 , 0
9 , 0
10 , 1930</pre>```


### ![Cc967423.collapse\_all(en-us,AX.60).gif](images/Gg863931.collapse_all(en-us,AX.60).gif "Cc967423.collapse_all(en-us,AX.60).gif")C\# Sample

``` csharp
using System;
public class Pgm_CSharp
{
    static public void Main( string[] args )
    {
        new Pgm_CSharp().Rs005a_CSharp_ArraySimple();
    }
    private void Rs005a_CSharp_ArraySimple()
    {
        const int const_iMacroArrayLength = 3;

        // In C# the length is set at construction during run.
        string[] sSports;
        int[] years;

        int xx;
        Console.WriteLine("-------- SPORTS --------");
        sSports = new string[const_iMacroArrayLength];
        sSports[const_iMacroArrayLength - 1] = "Baseball";

        for (xx=0; xx < const_iMacroArrayLength; xx++)
        {
            Console.WriteLine( xx.ToString()
                + " , [" + sSports[xx] + "]" );
        }
        Console.WriteLine("-------- YEARS --------");
        // In C# you must construct the array before assigning to it.
        years = new int[10];
        years[ 4] = 2008;
        years[10 - 1] = 1930;

        for (xx=0; xx < 10; xx++)
        {
            Console.WriteLine( xx.ToString()
                + " , [" + years[xx].ToString() + "]" );
        }
    }
} // EOClass

Cc967423.collapse_all(en-us,AX.60).gifOutput

The output from the C# program to the command line console is as follows:

0 , []
1 , []
2 , [Baseball]
-------- YEARS --------
0 , [0]
1 , [0]
2 , [0]
3 , [0]
4 , [2008]
5 , [0]
6 , [0]
7 , [0]
8 , [0]
9 , [1930]</pre>```


## Additional X++ Features

The container is a special data type that is available in X++. It can be considered as similar to an array, or similar to a List collection. For more information, see [Containers](containers.md).

## See also

[X++, C\# Comparisons](x-csharp-comparisons.md)

  
**Announcements:** New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the [MS Press Store](https://www.microsoftpressstore.com/store/inside-microsoft-dynamics-ax-2012-r3-9780735685109).