tags:

views:

838

answers:

6

Our coding standards ask that we minimise the use of C# var (suggests limiting it's use to being in conjunction with Linq). However there are times when using generics where it's reasonably convenient e.g.

Dictionary<DateTime, Dictionary<string, float>> allValues = ...
// ...
foreach (var dateEntry in allValue)

is easier to type

foreach (KeyValue<DateTime, Dictionary<string, float>> dateEntry in allValue)

(and easier than remembering what the explicit type is in some cases).

Do any of the refactoring tools have the ability to convert the former to the latter. I've had a look at Resharper but it doesn't seem to do (indeed it's default suggestion is to go in the opposite direction).

+3  A: 

Resharper does this (only in version 4+ I'm sure of it) but I don't know if its possible to simply convert var into the specific type automatic - eventually you need to go to the "var" and then a light shows up which gives you the option to specify the type explicitly

Calamitous
+10  A: 

I've got ReSharper 4.1, and it does offer this option (in either direction).

Actually, I'd recommend challenging the standard... the former is far more readable than the latter (especially if you call the variable pair or something similar). I would't use "var" for var i = 0, but it is ideally suited to the above.

For bulk changing, go to:

  • Cleanup Code... (pick a profile => "Edit Profiles" => Tools => Code Cleanup)
    • C#
      • Use 'var' in declaration
        • Replace direction = Can 'var' to type usage
        • 'foreach' iterator declaration style = Always use explicit type
        • Local variable declaration style = Always use explicit type

and run...

Marc Gravell
nice you found how to do it in bulk :) +1
Calamitous
Thanks, I'm new to Resharper and hadn't found it.
Ian G
A: 

I'd argue that where you have complex generic types on your domain objects you should typedef them

class DomainValueCollection : KeyValue<DateTime, Dictionary<string, float>>{}

where DomainValueCollection is somehting that makes sense in your domain context

the code then becomes

var allItems = new DomainValueCollection();

rather than

var allItems = new KeyValue<DateTime, Dictionary<string, float>>();
adam straughan
Not sure that will work; see my second post for why (it was too long to fit in a comment sensibly)
Marc Gravell
+2  A: 

@Adam - you can't declare a class here, since that won't be what comes back from an IDictionary<,>.GetEnumerator() - however, you can alias via using:

using InnerPair = System.Collections.Generic.KeyValuePair<System.DateTime,
    System.Collections.Generic.Dictionary<string, float>>;

Then:

foreach(InnerPair pair in dict) {...}

This is just a type alias, so works fine.

Marc Gravell
WHAT? why do all that when you can simply "var"...? I don't understand?
Bobby Cannon
@Bobby to match the question requirements...
Marc Gravell
+1  A: 

Why is it easier to type all that craziness? If you forgot what "var" was... just mouse over it in Visual Studio and it tells you the type.

Timothy Khouri
+1  A: 

DevExpress supports this with CodeRush, and they recently starting giving some of the refactorings away for free. The refactoring you are looking for is "Make Explicit"

http://devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/

Bob