Dictionary.Get(key, var Interface) overload causes CLR CS1503 error: interface argument passed by value instead of ByRef
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
- Declare a global Dictionary of [Text, Interface "IMyInterface"]
- 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