Freigeben über


X++, C# Comparison: Hello World

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 the simplest X++ program to its counterpart in C#.

X++ to C# Comparisons

The following sections describe some basic similarities and differences between X++ and C#.

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

The following X++ features are the same for C#:

  • Single line (//) and multi-line (/* */) comments.

  • == (equal) operator for determining whether two values are equal.

  • != (not equal to) operator for determining whether two values are not equivalent.

  • + (plus sign) operator for string concatenation.

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

The following table lists X++ features that are different in C#.

Features

X++

C#

Comments

Declarations

All declarations must be at the start of the method, before any X++ statements.

Declarations can occur anywhere in the method.

Both languages permit multiple variables of the same type to be listed together in one declaration. Both languages allow you to assign an initial value in the declaration statement.

if and else conditional statements

The if statement accepts any type of expression that it can automatically convert to a Boolean. Common examples include an int for which 0 means false, or an object for which null means false.

The if statement requires a Boolean expression.

The syntax structure regarding curly braces and parentheses is exactly the same between X++ and C#.

Literal string

A literal string can be delimited by either of the following:

  • A pair of double quotation mark (") characters.

  • A pair of single quotation mark (') characters.

A literal string must be delimited by a pair of double quotation mark (") characters.

For X++, the double quotation mark characters are usually used to delimit strings. However, it is convenient delimit a string with single quotation mark characters when your string must contain a double quotation mark character.

char type

There is no char or character type in X++. You can declare a str of length one, but it is still a string:

str 1 myString = "a";

There is a char in C#. You cannot pass a char as the parameter to a method that inputs a string parameter, although you can first explicitly convert the char to a string.

For more information about X++ data types, see Primitive Data Types.

Output of messages

X++ delivers messages to the user in the Infolog window. Common methods include the following:

  • The print statement:

  • static methods on the Global class:

    • Global::info

    • Global::warning

    • Global::error

For a command line C# program, messages can be delivered to the console. Common methods include the following:

  • Console.Out.WriteLine

  • Console.Error.WriteLine

The print statement is not a function nor a method. Recommended use would be print mystring; rather than print(mystring);. A pause; statement is always useful shortly after a print statement. The print statement is convenient for testing because it automatically converts int and other primitive values to strings for display. For more information, see Print Statements.

The Global class has special recognition in the X++ compiler. The info method can be called without including the Global:: prefix.

For more information, see, Global::info Method.

X++ and C++ Samples

This section contains two simple code samples. One sample is written in X++, and the other is in C#. Both samples achieve the same result.

The following X++ features are demonstrated:

  • // single line comment

  • /* */ multi-line comment

  • if statement

  • == operator

  • != operator

  • + operator to concatenate strings

  • Global::info for message output, with and without the Global:: prefix

  • Global::error for message output

  • The use of single and double quotation characters (' and ") as string delimiters.

    Note

    The best practice is to use double quotation marks for any string that might be displayed to the user.

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

This X++ code sample is in the form of a job. There is a node titled Jobs in the Application Object Tree (AOT). This sample can be added under the Jobs node, and then the job can be run.

static void JobRs001a_HelloWorld(Args _args)
{
    if (1 == 1) 
    {
        // These two info() calls are identical to the X++ compiler.
        // The second form is the one typically used in X++.
        Global::info("Hello World, 1.");
        info('Hello World, 2.');
    }
    
    if (1 != 1)
    {
        error("This message will not appear.");
    }
    else
    {
        // These two methods are also from the Global class.
        // The '+' operator concatenates two strings.
        warning("This is like info," + " but is for warnings, 3.");
        error("This is like info," + " but is for errors, 4.");
    }
}

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

Here is the actual output from the Infolog window:

Hello World, 1.
Hello World, 2.
This is like info, but is for warnings, 3.
This is like info, but is for errors, 4.</pre>```


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

The following C\# program is a rewrite of the previous X++ program. The differences between X++ and C\# are highlighted by commenting out the X++ lines, and replacing them with the C\# syntax.

``` csharp
using System;
class Pgm_CSharp
{
    static void Main( string[] args )
    {
        new Pgm_CSharp().Rs001a_CSharp_HelloWorld();
    }
    
    void Rs001a_CSharp_HelloWorld()
    {
        if (1 == 1) 
        {
            Console .Out .WriteLine(
                "Hello World, Explicit .Out , 1.");
            Console .WriteLine(
                "Hello World, Implicit default to .Out , 2.");
        }
    
        if (1 != 1)
        {
            Console .Error .WriteLine(
                "This message will not appear.");
        }
        else
        {
            Console .Error .WriteLine(".Error is like .Out,"
                + " but can be for warnings, 3.");
            Console .Error .WriteLine(".Error is like .Out,"
                + " but is for errors, 4.");
        }
    }
}

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

Here is the actual output to the C# console:

Hello World, Explicit .Out , 1.

Hello World, Implicit default to .Out , 2.

.Error is like .Out, but can be for warnings, 3.

.Error is like .Out, but is for errors, 4.

See also

X++, C# Comparisons

Relational Operators

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