Refactorings for implicitly-typed local variables

September 27th, 2011

Implicitly-typed local variables are variables declared without specifying the type explicitly. The type of such variables is being inferred from the expression that is used to initialize the variable at the time the code is compiled. Implicitly-typed variables are really useful for LINQ that creates anonymous types in queries, and for which you want to assign variables. However, implicitly-typed locals can be used with any variable declaration to enhance the readability, for example:

This implicitly-typed local variable will have the ‘double’ type. Or, better yet, this sample:

This code does not look impressive. If you don’t use implicitly-typed variables, you are forced to type the entire type: Dictionary<String, List<Int>>, instead of simply ‘var‘:

Make Implicit

Converts an explicitly-typed variable declaration to an implicit one. This is a simple refactoring which removes the type of a variable and makes it an implicitly-typed variable:

But sometimes, it does not only removes the type of a variable, but also converts an expression, so the compiler infers the type correctly without changing the logic of the code. Consider the following declaration:

or this:

Applying the Make Implicit will result in producing the following code:

and

Make Explicit

Converts the implicitly-typed local variable to a variable with an explicit type. This refactoring determines the type of the implicitly-typed variable before you compile the code and explicitly specifies it:

There is also an advanced version of the Make Explicit refactoring called Make Explicit (and Name Anonymous Type). This refactoring converts the implicitly-typed local variable to a variable with an explicit type, then creates a named type to represent the expression of an anonymous type declaration, and replaces the anonymous type with a newly-declared type. To learn more about this refactoring, you may read the corresponding topic about refactorings for anonymous types.

Both Make Implicit and Make Explicit refactorings are available in CSharp and Visual Basic programming languages where implicitly-typed variable declarations are allowed starting from Visual Studio 2008 and the corresponding language versions.

Make implicit is one of my favorite refactorings in the suite. Unfortunately, this opinion is not shared with the folks who set the coding conventions in one of the projects I work on. So, naturally, rather than changing my coding style, I’ve written a DXCore plugin that automates the conversions. It takes care of some capitalization and other naming conventions, and it makes *most* implicit declarations explicit.

Unfortunately, the only way I’ve found to do this is on a case-by-case basis. Do you know if there’s a way, using the API, to conceptually do “for all implicit in code file, make explicit”? It’d be a life-saver (well, lines of code saver) if you did.

Sure, you can iterate a source file and find all implicit variables, then call the Make Explicit refactoring on each declaration. There are two corresponding articles, to learn more on how to achieve that:

Perfect! That performing an existing refactoring post is *exactly* what I was looking for. I should be able to iterate through all declarations and use that, hopefully. Either way, when you release the version with that new refactoring, my code for this looks like it’ll drop to a line or two.