Resharper Refactoring Changes Behavior


Resharper is a great and must-have tool (plugin) for Visual Studio. It improves the code quality and helps to identify potential problematic code. The ‘Refactoring’ feature is great but the following situation I believe that it changes the intended behavior and should be paid attention carefully.

We have the following C# code, where a struct Point with one integer element is declared.

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
using System;
 
namespace ResharperRefDemo
{
    class Program
    {
        struct Point
        {
            public int x { get; set; }
        }
 
        public void foo1()
        {
            Point a = new Point();
            a.x = 1;
            Console.Write(a.x);
        }
 
        static void Main(string[] args)
        {
           var obj = new Program();
           obj.foo1();
        }
    }
}
using System;

namespace ResharperRefDemo
{
    class Program
    {
        struct Point
        {
            public int x { get; set; }
        }

        public void foo1()
        {
            Point a = new Point();
            a.x = 1;
            Console.Write(a.x);
        }

        static void Main(string[] args)
        {
           var obj = new Program();
           obj.foo1();
        }
    }
}

The next step, if we right click the line a.x = 1 and navigate Refactoring – Extract Method…

resharper-demo-refactoring-2 Resharper Refactoring Changes Behavior c # programming languages resharper Visual Studio

resharper-demo-refactoring-2

The following dialog pops up, offering us to extract the content into a separate function without the ref or out keywords. This should be fine for objects since they are passed by references in C#. But for structs, they are passed by values.

resharper-demo-refactoring-3 Resharper Refactoring Changes Behavior c # programming languages resharper Visual Studio

resharper does not offer ref or out keywords on refactoring

If you continue the refactoring (method extraction), you will get the following code, which is not right.

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
29
30
using System;
 
namespace ResharperRefDemo
{
    class Program
    {
        struct Point
        {
            public int x { get; set; }
        }
 
        public void foo1()
        {
            Point a = new Point();
            GetValue(a);
            Console.Write(a.x);
        }
 
        private static void GetValue(Point a)
        {
            a.x = 1;
        }
 
        static void Main(string[] args)
        {
           var obj = new Program();
           obj.foo1();
        }
    }
}
using System;

namespace ResharperRefDemo
{
    class Program
    {
        struct Point
        {
            public int x { get; set; }
        }

        public void foo1()
        {
            Point a = new Point();
            GetValue(a);
            Console.Write(a.x);
        }

        private static void GetValue(Point a)
        {
            a.x = 1;
        }

        static void Main(string[] args)
        {
           var obj = new Program();
           obj.foo1();
        }
    }
}

The original program prints number 1 while the output was changed to 0 by Resharper after Refactoring. However, for other simple types, int, float, double, resharper knows to offer the options of out or ref.

So, to sum up, be careful with refactoring, don’t 100% trust the tool.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
443 words
Last Post: How to Put Signature on PDF Document/File ?
Next Post: How to Connect Laptop to Three Monitors?

The Permanent URL is: Resharper Refactoring Changes Behavior

Leave a Reply