New resources available on Microsoft Learn
Did you know that Microsoft Learn offers free training modules to assist you on your path to mastering Dynamics 365 for Finance and Operations? Become an expert at your own pace or share with your team to foster growth.
Dynamics 365 2019 release wave 2 plan Discover the latest updates to Dynamics 365.Release Plan | Weekly Deployment Notes
Ace your Dynamics 365 deployment with packaged services delivered by expert consultants.Explore service offerings
Connect with the ISV success team on the latest roadmap, developer tool for AppSource certification, and ISV community engagements.
ISV self-service portal
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Program | Finance and Operations TechTalks | Customer Engagement TechTalks | Talent TechTalks
This is the new language feature that is generating most discussions. Just like C# you can now use the var keyword when declaring variables. It has the exact same semantics, and any guideline you will find on how to use it in C# will also apply in X++.
In C# the var keyword was introduced to enable scenarios where declaring the type was impossible, near-impossible or irrelevant, like with anonymous types and generics. Writing less was naturally well received by a lot of developers, and the use of var blossomed. It is also a great topic for steering up a debate. There are lots of guidelines and opinions. They cover the entire range from one extreme: "Always use it, it makes code easier to read" to the opposite extreme: "Never use it, it makes code harder to read".
X++ does not support anonymous types or generics in AX7 - so the real need for the var keyword is not present - yet it is supported, and already heavily used. Like in C# the use of var is as strongly typed as when the type is explicitly provided.
Personally, I'm recommending using var in only two cases:
MyClass myClass = new MyClass();
In AX7 I would write:
var myClass = new MyClass();
Sometimes you don't care about the type. If you don't care the reader most likely doesn't either.
ListEnumerator enumerator = myCollection.getEnumerator();
var enumerator = myCollection.getEnumerator();
There are a number of X++ specific reasons to not use var (i.e. doesn't apply to C#)
Just like the C# compiler, the X++ compiler will determine the type from the assignment to the variable and enforce all the same rules and restrictions. So you might think that the use of var doesn't lead to more runtime errors. There is a catch in X++ - you should be aware of. The X++ compiler doesn't enforce any validation of method invocations on object or common. So if the X++ compiler determines the type to be object or common, then you lost your safety net. It is not always obvious.
Consider this code:
CustTable custTable = Xml2Record(xml);
custTable.someInvalidMethod(); // Compile error
Using var, the code becomes:
var custTable = Xml2Record(xml);
custTable.someInvalidMethod(); // No compile error, as Xml2Record() returns a common
THIS POST APPLIES TO MICROSOFT DYNAMICS AX7 PREVIEW; IS PROVIDED AS-IS AND CONFERS NO RIGHTS.
Business Applications communities