X++, C# Comparison: Precompiler Directives
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
X++ and C# share some keywords for their precompiler directive syntax, but the meanings are not always the same.
X++ to C# Comparisons
The following sections describe the similarities and differences between the precompiler directives used in X++ and C#.
Similarities
The X++ and C# compilers recognize many of the same keywords. In most cases, the keywords mean the same for both language compilers.
Differences
A fundamental difference between the precompiler directives in X++ versus C# is the #define keyword that both language precompilers recognize. Unlike C#, in X++ the #define directive requires a dot in its syntax. In X++, parentheses can be used to give the defined symbol a value. These differences are shown in the following examples:
In X++: #define.InitialYear(2003)
In C#: #define InitialYear
A minor difference is that in C# there can be spaces and tab characters between the # character and the directive keyword, such as # define Testing.
Comparison Tables
The following tables compare the details of precompiler directives between X++ and C#.
Identical Keywords
The following table lists precompiler directives that are similar in X++ and C#.
Keyword |
X++ |
C# |
Comments |
---|---|---|---|
#define |
In X++, a precompiler variable name can be defined, and a value can be given to that variable. |
In C#, a precompiler variable name can be defined, but no value can be given to that variable. Also, any #define in C# must occur at the top of the file, and cannot occur after any code such as a using statement or a class declaration. |
The C# compiler can input a command line parameter of /define to define a precompiler variable name without defining the variable in any C# code file. The X++ compiler has no counterpart to /define. |
#if |
In X++, #if can determine whether a precompiler variable exists, and whether the variable has a given value. |
In C#, #if can only determine whether a precompiler variable exists. It cannot test for any value because no value can be assigned. |
|
#endif |
In X++, #endif marks the end of an #if block. It also ends an #ifnot block. |
In C#, #endif marks the end of an #if block, regardless of whether the block includes a #else. |
Different Keywords with the Same Processing Result
The following table lists precompiler directives that are named differently in X++ and C#, but that give the same results when processed.
X++ |
C# |
Comments |
---|---|---|
#ifnot |
#if #else |
There is no #else directive in X++, but the #ifnot provides similar functionality. In X++, #ifnot can determine whether a precompiler variable exists, and whether the variable does not have a specific given value. In C#, #if can determine whether a precompiler variable exists when the ‘!’ symbol is prefixed to the variable name. |
//BP Deviation documented |
#pragma warning |
These X++ and C# entries are not equivalent, but there is a partial similarity. Both suppress compiler warning messages. |
#macrolib |
.HPP file in C++ |
There is a partial similarity between the X++ directive #macrolib versus an .HPP file in C++. Both can contain several #define statements. |
Precompiler Directives Exclusive to X++
The following table lists X++ precompiler directives that have no direct counterpart in C#.
X++ |
Comments |
---|---|
#linenumber |
The #linenumber directive is for obtaining the line number, so that it can be output to the Infolog. The C# directive #line is different because its purpose is for setting the line number. |
#defdec #definc |
|
#globaldefine |
In X++, there is a small difference between #globaldefine versus #define. The difference is that #globaldefine never overwrites a current nonnull value that was assigned to a precompiler variable by #define. C# has nothing similar to this difference, because in C#, a precompiler variable name cannot be given a value. |
#localmacro #macro |
In X++, #localmacro enables you to assign a multiline value to a precompiler variable. #macro is a synonym, but #localmacro is recommended. In C#, the #define directive has part of this functionality, but it cannot assign a value to a precompiler variable. |
#globalmacro |
In X++, #globalmacro is almost the same as the preferred #localmacro. |
See also
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.