X++, C# Comparison: Object Oriented Programming

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

The object oriented programming (OOP) principles of X++ differ from C#.

Conceptual Comparisons

The following table compares the implementation of OOP principles between Microsoft Dynamics AX X++ and C#.

Feature

X++

C#

Comments

Casting

Starting with Microsoft Dynamics AX 2012 the X++ language has the keywords is and as, which are used to make downcasts safe and explicit.

Note

In Microsoft Dynamics AX 2012 the X++ language does not require the use of the as keyword when you downcast a base class variable to a derived class variable. However, we recommend that all downcast statements use the as keyword.

An object can be cast either up or down the inheritance path. Downcasts require the as keyword.

For more information about the X++ keywords is and as, see Expression Operators: Is and As for Inheritance.

Local functions

A method can contain a declaration and code body for zero or more local functions. Only that method can have calls to the local function.

C# 3.0 supports lambda expressions, which have some similarity to anonymous functions and local functions. Lambda expressions are often used with delegates.

For more information about local functions in X++, see Local Functions.

Method overloading

Method overloading is not supported. A method name can occur only one time per class.

Method overloading is supported. A method name can occur multiple times in one class, with different parameter signatures in each case.

X++ does support optional parameters on methods. Optional parameters can partially mimic method overloading. For more information, see the row for optional parameters in this table.

Method overriding

Method overriding is supported. A derived class can have a method by the same name as in the base class, as long as the parameter signature is the same in both cases. The only exception is that the overriding method can add a default value to a parameter.

Method overriding is supported. The virtual keyword must be applied to a method before the method can be overridden in a derived class.

The concept of overriding a method includes the method name, its parameter signature, and its return type. The concept of method overriding does not apply if the base method and the overriding method differ in any of these aspects.

Optional parameters

A parameter declaration can be followed by a default value assignment. The method caller has the option of passing a value for that parameter, or ignoring the parameter to accept the default value. This feature mimics method overloading because two calls to the same method name can pass different numbers of parameters.

Each parameter that has a default value must follow the last parameter that does not have a default value.

Optional parameters are supported by the params keyword. Even without the params keyword, from the point of view of the caller, method overloading can provide partially similar functionality.

For more information, see Parameters and Scoping and Using Optional Parameters.

Single inheritance

You can derive your X++ class from another X++ class by using the extends keyword in the classDeclaration node of your class, in the AOT. No class implicitly derives directly from another class. If you want your class to directly derive from the Object class, you must use the extends keyword. You can specify only one class on the extends keyword.

Caution

When you modify an X++ base class that other classes derive from, you must recompile that base class using the Compile forward. This option ensures that the derived classes are also recompiled. To ensure the derived classes are also recompiled, right-click the base class node, and then click Add-Ins > Compile forward. The alternative of clicking Build > Compile (or pressing the F7 key) is sometimes insufficientfor a base class change.

A class can implement zero to many interfaces.

An X++ table implicitly inherits from the Common table, and from the xRecord class.

C# uses the extends keyword to derive from another class. All .NET Framework classes implicitly derive from the System.Object class, unless they explicitly derive from another class.

For more information about X++ interfaces, see Interfaces Overview.

Keyword Comparisons

The following table lists the OOP-related keywords in X++. The usage of each keyword is compared between X++ and C#.

Keyword

X++

C#

Comments

abstract

No difference.

class

The modifiers public and private are ignored on class declarations.

There is no concept of a namespace grouping of classes. There are no dots (.) in any class names.

The modifiers public and private can be used to modify class declarations. C# also has the keyword internal, which relates to how classes are grouped together in assembly files.

There is no concept of a protected class, only protected members of a class.

extends

A class declaration can inherit from another class by using the extends keyword.

A colon (:) is used where the keywords extends and implements are used in X++.

final

A final method cannot be overridden in a derived class. A final class cannot be extended.

The keyword sealed on a class means the same thing that final means on an X++ class.

implements

A class declaration can implement an interface by using the implements keyword.

(See extends.)

interface

An interface can specify methods that the class must implement.

An interface can specify methods that the class must implement.

new

The new keyword is used to allocate a new instance of a class. Then the constructor is automatically called.

Each class has exactly one constructor, and the constructor is named new. You can decide what parameters the constructor should input.

The new keyword is used to create a new instance of a class. Then the constructor is automatically called.

Constructor methods themselves are not named new, they have the same name as the class.

Note

The new keyword can also be used on a method, to modify the way in which the method overrides the same method in the base class.

Both X++ and C# assume a default constructor for classes that have no constructor explicitly written in their code.

null

No difference.

private and protected

The private and protected keywords can be used to modify the declaration of a class member.

The private and protected keywords can be used to modify the declaration of a class member.

For more information, see Method Access Control.

public

A method that is not modified with public, protected, or private has the default access level of public.

A method that is not modified with public, protected, or private has the default access level of private.

(For more information, see private.)

static

A method can be static, but a field cannot.

Both methods and fields can be static.

super

The super keyword is used in a derived class to access the same method on its base class.

void method2()

{

;

// Call method2 method

// on the base class.

super();

}

The base keyword is used in a derived class to access various methods in its base class.

void method2()

{

// Call methods on

// the base class.

base.method2();

base.method3();

}

In C#, there is special syntax for using base to call the base constructor.

this

For a call from one instance method to another on the same object, a qualifier for the called method is required. The keyword this is available as a qualifier for the current object.

For a call from one instance method to another on the same object, a qualifier for the called method is not required. However, the this keyword is available as a qualifier for the current object. In practice, the keyword this can be helpful by displaying IntelliSense information.

finalize

The Object class contains the finalize method. The finalize method is not final, and it can be overridden.

The finalize method appears to resemble the System.Object.Finalize method in C#, but in X++ the finalize method has no special meaning of any kind.

An object is automatically removed from memory when the last reference to the object stops referencing the object. For example, this can happen when the last reference goes out of scope or is assigned another object to reference.

The methods Finalize and Dispose are common on some types of classes.

The garbage collector calls the Finalize and Dispose methods when it destroys and object.

In C#, the System.GC.Collect method in the .NET Framework can be called to start the garbage collector. There is no similar function in X++ because X++ uses a deterministic garbage collector.

main

Classes that are invoked from a menu have their main method called by the system.

Classes that are invoked from a command line console have their Main method called by the system.

See also

X++, C# Comparisons

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