Home > Refactorings > Refactorings for implicitly-typed local variables

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:

Refactor! Implicit variable sample

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

Refactor! Make Implicit sample code (Dictionary)

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‘:

Refactor! Make Implicit result (Dictionary)

DevExpress Refactor! Pro and CodeRush Xpress has two refactorings, that allow you to convert from the implicitly-typed variables into explicitly-type variables, and vice versa. Here they are:

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:

Refactor! Make Implicit preview

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:

Refactor! Make Implicit sample code (ulong)

or this:

Refactor! Make Implicit sample (int array)

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

Refactor! Make Implicit result (ulong)


Refactor! Make Implicit result (int array)

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:

Refactor! Make Explicit preview

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.

Products: Refactor! Pro, CodeRush Xpress
Versions: 11.1 and up
VS IDEs: any
Updated: Sep/27/2011
ID: R039

Similar Posts:

  1. September 28th, 2011 at 08:35 | #1

    Hey —

    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.


  2. September 28th, 2011 at 08:53 | #2

    @ Erik

    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:

    How to enumerate solution and source code items using DXCore

    How to perform an existing refactoring from another one

    If you need any assistance – let me know.

    Also, we are going to implement this functionality in the context of the following suggestion: S38160.

  3. September 29th, 2011 at 07:48 | #3

    @ Alex Skorkin

    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.

    Thanks! 😎

  4. September 29th, 2011 at 08:03 | #4

    @ Erik
    Glad to hear that, Erik!
    Thanks for the update!