AL icon indicating copy to clipboard operation
AL copied to clipboard

Dictionary.Get(key, var Interface) overload causes CLR CS1503 error: interface argument passed by value instead of ByRef

Open arsag opened this issue 7 months ago • 0 comments

1. Describe the bug Using the two-argument Dictionary.Get(Key, var Value) overload where the value type is an Interface causes a CLR CS1503 compilation error at runtime. The AL compiler accepts the code without warnings, but the server-side C# compilation fails because the interface handle is passed by value instead of ByRef.

2. To Reproduce

  1. Declare a global Dictionary of [Text, Interface "IMyInterface"]
  2. Call the two-argument Get overload, passing a var Interface parameter as the output argument
codeunit 50000 "My Codeunit"
{
    var
        MyDict: Dictionary of [Text, Interface "IMyInterface"];

    local procedure TryGet(Key: Text[30]; var Result: Interface "IMyInterface"): Boolean
    begin
        exit(MyDict.Get(Key, Result)); // Triggers CLR CS1503 at runtime
    end;
}

3. Expected behavior Dictionary.Get(Key, var Value) should work for Interface value types, the same way it works for scalar types (Text, Integer, etc.), returning true/false and assigning the found value to the output parameter.

4. Actual behavior The AL compiler reports no error, but at runtime the server-side C# compilation fails with:

System.InvalidOperationException: C# compilation has failed for the application object CodeUnit_XXXXXXX. error CS1503: Argument 3: cannot convert from 'Microsoft.Dynamics.Nav.Runtime.NavInterfaceHandle' to 'Microsoft.Dynamics.Nav.Runtime.ByRef<Microsoft.Dynamics.Nav.Runtime.NavInterfaceHandle>'

The workaround is to split the call into ContainsKey + single-argument Get:

local procedure TryGet(Key: Text[30]; var Result: Interface "IMyInterface"): Boolean
begin
    if MyDict.ContainsKey(Key) then begin
        Result := MyDict.Get(Key);
        exit(true);
    end;
end;

5. Versions:

  • AL Language: 17.0
  • Visual Studio Code: 1.117
  • Business Central: 28
  • Operating System:
    • Windows

Internal work item: AB#634057

arsag avatar Apr 29 '26 15:04 arsag