Using Resharper – Turning to LINQ


Resharper is a popular but commercial tool that is used to make Visual Studio a better IDE (Integrated Development Environment). It helps to spot code errors/warnings almost instantly and therefore is a great and useful tool to improve the code quality. For example, the following C# code (Console Application) copies some elements to another, just for demonstration purpose:

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));
                }
            }
        }
    }
}

And, if you install Resharper (BTW, 30 day trial), it will look something like this:

resharper-bubble-hint Using Resharper - Turning to LINQ c # IDE

Resharper – Visual Studio Plugin

First, the resharper turns un-used using statements grey colour, so you know you can safely remove these statements. Second, the dashed lines under keywords or variables tell you that there are hints/information or warnings, so when you move the mouse onto it, the resharper plugin will tell you what to do.

For example, the declarations can be simplified using var keyword for object, complex structures.

1
2
var test = new List<string>();
var test2 = new List<Int32>();
var test = new List<string>();
var test2 = new List<Int32>();

It also tells you that the variable test2 is only updated but never used. The best part is that it can make code elegant by using LINQ, so the above foreach will be converted to LINQ using just 1 click.

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();

It also help spot many code warnings. This is a definitely worth-having tool for .NET programmers!

resharper-is-thinking Using Resharper - Turning to LINQ c # IDE

resharper-is-thinking

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
452 words
Last Post: Powershell Tutorial - Create COM object
Next Post: Simple Math Exercise - Computer V.S. Human

The Permanent URL is: Using Resharper – Turning to LINQ

Leave a Reply