Auto Encapsulate Field Refactoring, Difference between 'Use field' and 'Use Property'?
c#, visual-studio
Solution
In English, the options are called:
- Encapsulate field (and use property)
- Encapsulate field (but still use field)
The difference is in what it does to usages of the field. The first option will update all usages of that field to use the new properties that it creates. The second option doesn't change existing usages of the field elsewhere in your code.
So if elsewhere you have this code:
var test = new Test_EncapsulateFieldRefactoring_Property();
test.name = "Hello";
You'll find that the first option updates `test.name` to the new `test.Name` property, but the second option doesn't.
Problem
On Visual Studio 2017, I have two options when using the Auto Encapsulate Field Refactoring Tool: - Use Property - Still use field I have tested the different option on a basic class: ``` public class Test_EncapsulateFieldRefactoring_Property { public int id; public string name; } ``` But both option gave the same result: ``` public class Test_EncapsulateFieldRefactoring_Property { private int id; private string name; public int Id { get => id; set => id = value; } public string Name { get => name; set => name = value; } } ``` Why do those options exist? Where is the difference (in code generated , "useage"*)? Disclamer: - The screenshot is a on French VS. So option translations are made by me, real option text may differ. - I know the difference between field and property. I have checked a lot of topics to see if it was not a dupe. I could have missed one. - *, Can't find a good translation for this one: "in the way you use it". But in this context not the difference in use between as field and property but in the menu option.