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.
Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.
Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.