X++, C# Comparison: String Case and Delimiters
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 treatment of strings with mixed casing in X++ and C#. It also explains the string delimiters that are available in X++.
X++ to C# Comparisons
There are similarities and differences in how strings are delimited in X++ and C#.
Similarities
The following X++ features are the same as in C#:
The backslash (\) is the escape operator for string delimiters.
The at sign (@) nullifies the escape effect of the backslash when the at sign is written immediately before the open quotation mark of a string.
The plus sign (+) is the string concatenation operator.
Differences
X++ features that are different in C# are listed in the following table.
Feature |
X++ |
C# |
Comments |
---|---|---|---|
== comparison operator |
Insensitive: the == operator is insensitive to differences in string casing. |
In C#, the == operator is sensitive to differences in string casing. |
In X++ you can use the strCmp Function for case sensitive comparisons between strings. |
String delimiters |
In X++ you can use either the single (') or double (") quotation mark as the string delimiter. Note Usually the best practice is to use double quotation marks for strings that might be displayed to the user. However, it is convenient to delimit a string with single quotation marks when a double quotation mark is one of the characters in the string. |
In C# you must use the double quotation mark as the string delimiter. This refers to the type System.String. |
In X++ and C# you have the option of embedding a delimiter in a literal string and escaping it with \. In X++ you also have the alternative of embedding single quotation marks in a string that is delimited by double quotation marks (or the reverse), without having to use the escape. |
Character delimiters |
X++ has a string data type (str), but no character type. |
In C# you must use the single quotation mark as the character delimiter. This refers to the type System.Char. |
In the .NET Framework, a System.String of length one is a different data type than a System.Char character. |
Example 1: Case Sensitivity of the == Operator
The == and != operators are case insensitive in X++, but are case sensitive in C#, as is illustrated by the following example.
X++ |
C# |
Comments |
---|---|---|
"HELLO" == "hello" True in X++. |
"HELLO" == "hello" False in C#. |
Different case comparisons between X++ and C#. |
Example 2: The + String Concatenation Operator
The + and += operators are used to concatenate strings in both X++ and C#, as is shown by the examples in the following table.
X++ |
C# |
Comments |
---|---|---|
myString1 = "Hello" + " world"; Result is equality: myString1 == "Hello world" |
(Same as for X++.) |
In both X++ and C#, the behavior of the + operator depends on the data type of its operands. The operator concatenates strings, or adds numbers. |
mystring2 = "Hello"; myString2 += " world"; Result is equality: myString2 == "Hello world" |
(Same as for X++.) |
In both X++ and C#, the following statements are equivalent: a = a + b; a += b; |
Example 3: Embedding and Escaping String Delimiters
Either single or double quotation marks can be used to delimit strings in X++. The escape character (\) can be used to embed delimiters in a string. These are illustrated in the following table.
X++ |
C# |
Comments |
---|---|---|
myString1 = "He said \"yes\"."; Result: He said "yes". |
(Same as for X++.) |
The escape character enables you to embed string delimiters inside strings. |
myString2 = 'He said "yes".'; Result: He said "yes". |
C# syntax does not allow for single quotation marks to delimit strings. |
For strings that may be seen by the user, it is considered a best practice to use the escape character instead of the single quotation marks as shown in the example. |
myString3 = "He said 'yes'."; Result: He said 'yes'. |
(Same as for X++.) |
In X++, the single quotation marks are not treated as delimiters unless the string starts with a single quotation mark delimiter. In C# the single quotation mark has no special meaning for strings, and it cannot be used to delimit strings. In C# the single quotation mark is the required delimiter for literals of type System.Char. X++ has no character data type. |
str myString4 = 'C'; Here the single quotation is a string delimiter. |
char myChar4 = 'C'; Here the single quotation mark is a System.Char delimiter, not a System.String delimiter. |
X++ has no data type that corresponds to System.Char in the .NET Framework. An X++ string that is limited to a length of one is still a string, not a character data type. |
Example 4: Single Escape Character
Examples that illustrate the single escape character in either the input or the output are shown in the following table.
X++ |
C# |
Comments |
---|---|---|
myString1 = "Red\ shoe"; Result: Red shoe |
A literal string in C# cannot contain the two character sequence of escape followed by a space, such as "\ ". A compiler error occurs. |
When the X++ compiler encounters the two character sequence of "\ ", it discards the single escape character. |
myString2 = "Red\\ shoe"; Result: Red\ shoe |
(Same as for X++.) |
In a pair of escape characters, the first negates the special meaning of the second. |
See also
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.