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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With