Export (0) Print
Expand All
4 out of 6 rated this helpful - Rate this topic

X++, C# Comparison: Hello World [AX 2012]

Updated: November 16, 2010

Applies To: 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#.

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

Similarities

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.

Differences

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.

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.

    NoteNote

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

X++ 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.");
    }
}

Output

Here is the actual output from the Infolog window:

Message (09:49:48)
Hello World, 1.
Hello World, 2.
This is like info, but is for warnings, 3.
This is like info, but is for errors, 4.

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.

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

Output

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.