Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ReSharper need to scan all files when converting a property to an auto property?

Is there any difference between accessing a property that has a backing field

    private int _id;
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

versus an auto-property?

public int Id { get; set; }

The reason I'm asking is that when letting ReSharper convert a property into an auto property it seems to scan my entire solution, or at least all aspx-files.

I can't see any reason why there should be any difference between the two from outside the class. Is there?

like image 331
mflodin Avatar asked Mar 04 '11 17:03

mflodin


1 Answers

The compiler generates the backing field for Auto-Properties automatically, so no, there shouldn't be any difference.

ReSharper is scanning all the files, because if you have a Partial class defined, it could be using the backing field instead of the public property even though the code exists in physically different files.

For Example:

// MyClass.cs
public partial class MyClass
{
    int _id;
    public int ID { get { return _id; } set { _id = value; } }
    public MyClass(int identifier)
    {
        ID = identifier;
    }
}

// MyClass2.cs
public partial class MyClass
{
    public void ChangeID(int newID) 
    {
        _id = newID;
    }
}

ReSharper must scan all files, since it has no way to know where a partial class might be defined.

like image 119
Nate Avatar answered Oct 04 '22 06:10

Nate