Personalized Community is here!
Quickly customize your community to find the content you seek.
Latest TechTalk Videos
Have questions on moving to the cloud? Visit the Dynamics 365 Migration Community today! Microsoft’s extensive network of Dynamics AX and Dynamics CRM experts can help.
2021 Release Wave 2Discover the latest updates and new features releasing from October 2021 through March 2022.
2021 release wave 2 plan
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Program | Finance and Operations TechTalks | Customer Engagement TechTalks | Upcoming TechTalks | All TechTalks
As you may have noticed, you now have the possibility of using var declarations in X++. These declarations have the form:
var age = 28;
var fruitName = "Banana";
In other words, the compiler will calculate the type of the declared entity from the expression on the right hand side of the declaration assignment.
Note, that the entities that are declared in this way are still strongly typed; Intellisense and the compiler will know that value is of type int, and fruitName is of type str.
In this case, you can argue that there is no real benefit to using var. You could just as easily have written
int age = 28;
str fruitName = "Banana";
However, there are cases where declarations can become much more legible with this new syntax. Consider the following:
var c1 = new System.Collections.ArrayList();
System.Collections.ArrayList c2 = new System.Collections.ArrayList();
I think you will agree that the former is more legible than the latter.
Also, we will need var going forward in cases where the compiler will have to deal with types that are not actually describable in the core language.
There are cases where I would suggest not using the var syntax though. Imagine that you are a diligent developer that wants to code against contracts, rather than against implementations. You would therefore write:
IDrivable c = new Car();
because you are deliberately seeking to use only the methods available on the interface in your code. There is a similar case involving base classes. Assume the natural inheritance between animals, where a horse is-an animal. It would make perfect sense for a developer to do:
Animal a = new Horse();
The compiler will only allow methods on Animal to be called. If you instead used var here, the compiler would use the types on the right hand side, Car and Horse respectively, allowing the developer to access things that they probably shouldn't.
With these few caveats in mind, go forth and use var!
Business Applications communities