다음을 통해 공유


X++, C# Comparison: Switch

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 switch statement in X++ and C#.

X++ to C# Comparisons

In both X++ and C#, the switch statement involves the keywords case, break, and default.

The following table lists the differences in the switch statement between X++ and C#.

Feature

X++

C#

Comments

break; at the end of each case block

In X++, when any case block matches the expression value on the switch clause, all other case and default blocks are executed until a break; statement is reached.

No break; statement is ever required in an X++ switch statement, but break; statements are important in almost all practical situations.

In C#, a break; statement is always needed after the statements in a case or default block.

If a case clause has no statements between itself and the next case clause, a break; statement is not required between the two case clauses.

We recommend against omitting the break; statement after any case block, because it can confuse the next programmer who edits the code.

break; at the end of the default block

In X++ there is no effect of adding a break; statement at the end of the default block.

In C# the compiler requires a break; statement at the end of the default block.

For more information, see Switch Statements.

Only constant values on a case block

In X++ you can specify either a literal value or a variable on a case block. For example, you can write case myInteger:.

In C# you must specify exactly one literal value on each case block, and no variables are allowed.

No comments.

Multiple values on one case block

In X++ you can specify multiple values on each case block. The values must be separated by a comma. For example, you can write case 4,5,myInteger:.

In C# you must specify exactly one value on each case block.

In X++ it is better to write multiple values on one case block than to omit the break; statement at the end of one or more case blocks.

Code Examples for switch

The following sections show comparable switch statements in X++ and C#.

Cc967402.collapse_all(en-us,AX.60).gifX++ switch Example

The X++ switch example shows the following:

  • case iTemp: and case (93-90): to show that case expressions are not limited to constants, as they are in C#.

  • //break; to show that break; statements are not required in X++, although they are almost always desirable.

  • case 2, (93-90), 5: to show that multiple expressions can be listed on on case clause in X++.

    static void GXppSwitchJob21(Args _args)  // X++ job in AOT > Jobs.
    {
      int iEnum = 3;
      int iTemp = 6;
      
      switch (iEnum)
      {
        case 1:
        case iTemp:  // 6
          info(strFmt("iEnum is one of these values: 1,6: %1", iEnum));
          break;
    
        case 2, (93-90), str2Int("5"):  // Equivalent to three 'case' clauses stacked, valid in X++.
        //case 2:
        //case (93-90):  // Value after each 'case' can be a constant, variable, or expression; in X++.
        //case str2Int("5"):
          info(strFmt("iEnum is one of these values: 2,3,5: %1", iEnum));
          //break;  // Not required in X++, but usually wanted.
    
        case 4:
          info(strFmt("iEnum is one of these values: 4: %1", iEnum));
          break;
    
        default:
          info(strFmt("iEnum is an unforeseen value: %1", iEnum));
          break;
      
        // None of these 'break' occurrences in this example are required for X++ compiler.
      }
      return;
    }
    /*** Copied from the Infolog:
    Message (02:32:08 pm)
    iEnum is one of these values: 2,3,5: 3
    iEnum is one of these values: 4: 3
    ***/

Cc967402.collapse_all(en-us,AX.60).gifC# switch Example

The C# switch example shows the following:

  • case 1: has a comment explaining that only constant expressions can be given on a case clause.

  • break; statements occur after the last statement in each case block that has statements, as is required by C#.

using System;  // C#

namespace CSharpSwitch2
{
  class Program
  {
    static void Main(string[] args)  // C#
    {
      int iEnum = 3;

      switch (iEnum)
      {
        case 1:  // Value after each 'case' must be a constant.
        case 6:
          Console.WriteLine("iEnum is one of these values: 1,6: " + iEnum.ToString());
          break;

        //case 2,3,5:  // In C# this syntax is invalid, and multiple 'case' clauses are needed.
        case 2:
        case 3:
        case 5:
          Console.WriteLine("iEnum is one of these values: 2,3,5: " + iEnum.ToString());
          break;

        case 4:
          Console.WriteLine("iEnum is one of these values: 4: " + iEnum.ToString());
          break;

        default:
          Console.WriteLine("iEnum is an unforeseen value: " + iEnum.ToString());
          break;

        // All 'break' occurrences in this example are required for C# compiler.
      }
      return;
    }
  }
}
/*** Output copied from the console:
>> CSharpSwitch2.exe
iEnum is one of these values: 2,3,5: 3
>>
***/

See also

X++, C# Comparisons

Switch Statements

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