How to Use Reference Data Types in Microsoft Dynamics (Instead of Value Types)
Recently, I’ve come to learn a very interesting fact about data types (such as Lists and Dictionaries). For a little background:
- A List data type is defined as a strongly typed list of objects that can be accessed by index. You can check all the methods available for List data type.
- A Dictionary data type is defined as a collection of keys and values. You can check all the methods available for Dictionary data type.
The interesting part is that, for performance reasons, they are reference types (not value types).
In Microsoft Dynamics 365 Business Central, a reference type means that the variable does not hold a value in and of itself, but rather a reference to an instance of an object in memory.
When you pass this kind of variable as a parameter to a function, that function is receiving a reference to the object in memory and it will work directly on that object through the reference.
How to use reference data types | Example
Let me explain the concept with a simple example.
We can extract the list of overdue sales invoices using a List data type as shown in the following code:
Note that the parameter is not passed by var.
I will use a report to output for the results, for better organization.
As you can see, even if the parameter was not passed by var, it will come back with the results:
This happens because the List is a reference type. In both places, the report and the codeunit are pointing to the exact same object in memory.