本文介绍了将C#var重构为显式类型的工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的编码标准要求我们尽量减少C#var的使用(建议限制将其与Linq结合使用)。但是,有时候在使用泛型时会比较方便,例如。

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)

更容易键入

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

(而且比记住某些情况下的显式类型更容易)。

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

任何重构工具都可以将前者转换为后者。我看过Resharper,但似乎没做(实际上,默认建议是朝相反的方向走。)。

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).

推荐答案

我有ReSharper 4.1,它确实提供了这个选项(在任何一个方向上)。

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

实际上,我建议挑战该标准。 。前者比后者更具可读性(特别是如果您调用变量 pair 或类似的名称)。对于 var i = 0 ,我不会使用 var,但是它非常适合以上情况。

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:


  • 清理代码...(选择一个配置文件=>编辑配置文件 =>工具=>代码清除)


    • C#


      • 在声明
        中使用'var'

        • 替换方向=可以'var'键入用法

        • 'foreach'迭代器声明样式=始终使用显式类型

        • 局部变量声明样式=始终使用显式类型

        并运行...

        这篇关于将C#var重构为显式类型的工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:35