最近在评估公司需不需要一个 Visual Studio 的插件: Resharper. Resharper 是商业软件, 而且价钱不便宜, 一个一套 License 是200 镑左右. 这插件的目的就是把 VS 变得更强大好用. 比如以下C#代码将一个列表拷贝到另一个(只是演示用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<string> test = new List<string>(); List<Int32> test2 = new List<Int32>(); for (int i = 0; i < 100; i++) { test.Add(i.ToString()); } foreach (var i in test) { if (i == "3") { test2.Add(Int32.Parse(i)); } } } } } |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<string> test = new List<string>(); List<Int32> test2 = new List<Int32>(); for (int i = 0; i < 100; i++) { test.Add(i.ToString()); } foreach (var i in test) { if (i == "3") { test2.Add(Int32.Parse(i)); } } } } }
如果你装了Resharper(有30天试用), 应该会在界面上看到有一些变化.
首先, 没有用到的 using 就会变成灰的了, 很方便,很直观, 直接删掉就可以. 而且, 如果有些建议, 会在相应的关键字或者变量名下划波浪线或者是虚线, 鼠标移上去会有相应的说明.
比如在声明列表的(或对象, 其它复杂结构)可以用 var 关键字来简化.
1 2 | var test = new List<string>(); var test2 = new List<Int32>(); |
var test = new List<string>(); var test2 = new List<Int32>();
并告诉你变量 test2 只是被更新, 却没有被使用. 最惊奇的是会告诉你把一些 foreach 语句变成 LINQ, 鼠标只需要按一键就会进行代码转换.
1 2 3 4 5 6 | var test = new List<string>(); for (int i = 0; i < 100; i++) { test.Add(i.ToString()); } List<Int32> test2 = (from i in test where i == "3" select Int32.Parse(i)).ToList(); |
var test = new List<string>(); for (int i = 0; i < 100; i++) { test.Add(i.ToString()); } List<Int32> test2 = (from i in test where i == "3" select Int32.Parse(i)).ToList();
感觉很有意思, 当然如果代码里有警告, 也是会及时提醒, 能提高代码质量并相应的减少代码错误.
英文同步: https://helloacm.com/using-resharper-turning-to-linq/
GD Star Rating
loading...
本文一共 294 个汉字, 你数一下对不对.loading...
上一篇: 学习 PowerShell - 创建 COM 组件
下一篇: 回忆 2008 年伦敦新年倒计时
扫描二维码,分享本文到微信朋友圈