Wednesday, October 27, 2021

Var in x++

Usually, it's just a shortcut, so you don't have to declare the type if it would make the code less readable. For example, often the type is obvious or not important, and specifying it would make code less readable.


You're welcome to use var where it makes sense, but make sure that you don't make your code less readable by omitting important type information. That the type is obvious to the compiler doesn't mean it's obvious to developers.


In C#, it's also necessary for variables of anonymous types, because there is no way how to declare such a type. For example, you have a LINQ query returning two fields and the framework returns an object with these two fields, but it doesn't have any named type.


Anonymous types are going to be supported in X++ in the future.