Tuesday 1 March 2016

Extension methods AX7

The extension method feature lets you add extension methods to a target class by writing the methods in a separate extension class. The following rules apply:
    • The extension class must be static.
    • The name of the extension class must end with the ten-character suffix _Extension. However, there’s no restriction on the part of the name that precedes the suffix.
    • Every extension method in the extension class must be declared as public static.
    • The first parameter in every extension method is the type that the extension method extends. However, when the extension method is called, the caller must not pass in anything for the first parameter. Instead, the system automatically passes in the required object for the first parameter.
It’s perfectly valid to have private or protected static methods in an extension class. These are typically used for implementation details and are not exposed as extensions.
The example below illustrates an extension class holding a few extension methods:
public static class AtlInventLocation_Extension
{
  public static InventLocation refillEnabled(
    InventLocation _warehouse, 
    boolean _isRefillEnabled = true)
  {
    _warehouse.ReqRefill = _isRefillEnabled;
    return _warehouse;
  }

  public static InventLocation save(InventLocation _warehouse)
  {
    _warehouse.write();
    return _warehouse;
  }
}

No comments:

Post a Comment